package com.vladmihalcea.book.hpjp.hibernate.statistics;

import org.hibernate.stat.internal.ConcurrentStatisticsImpl;

import java.util.concurrent.atomic.AtomicLong;

/**
 * <code>TransactionStatistics</code> - Transaction Statistics
 *
 * @author Vlad Mihalcea
 */
public class TransactionStatistics extends ConcurrentStatisticsImpl {

    private static final ThreadLocal<AtomicLong> transactionStartNanos = new ThreadLocal<AtomicLong>() {
        @Override
        protected AtomicLong initialValue() {
            return new AtomicLong();
        }
    };

    private static final ThreadLocal<AtomicLong> connectionCounter = new ThreadLocal<AtomicLong>() {
        @Override
        protected AtomicLong initialValue() {
            return new AtomicLong();
        }
    };

    private StatisticsReport report = new StatisticsReport();

    @Override public void connect() {
        connectionCounter.get().incrementAndGet();
        transactionStartNanos.get().compareAndSet(System.nanoTime(), 0);
        super.connect();
    }

    @Override public void endTransaction(boolean success) {
        report.transactionTime(System.nanoTime() - transactionStartNanos.get().longValue());
        report.connectionsCount(connectionCounter.get().longValue());
        report.generate();
        transactionStartNanos.remove();
        connectionCounter.remove();
        super.endTransaction(success);
    }
}

@@ -380,28 +380,17 @@
 icLong%3E 
-transactionS
+s
 tartNano
@@ -438,32 +438,24 @@
    @Override
-%0A       
  protected A
@@ -654,24 +654,16 @@
 Override
-%0A       
  protect
@@ -904,36 +904,25 @@
 ();%0A        
-transactionS
+s
 tartNanos.ge
@@ -1050,24 +1050,42 @@
  success) %7B%0A
+        try %7B%0A    
         repo
@@ -1123,28 +1123,17 @@
 ime() - 
-transactionS
+s
 tartNano
@@ -1136,38 +1136,36 @@
 Nanos.get().
-longValue
+get
 ());%0A
+    
         repo
@@ -1212,17 +1212,11 @@
 t().
-longValue
+get
 ());
@@ -1220,24 +1220,28 @@
 ));%0A        
+ 
+   
 report.gener
@@ -1259,20 +1259,33 @@
     
-transactionS
+%7D finally %7B%0A            s
 tart
@@ -1300,32 +1300,36 @@
 move();%0A        
+  
+  
 connectionCounte
@@ -1332,32 +1332,42 @@
 unter.remove();%0A
+        %7D%0A
         super.en

Add finally block as well