How to log JDBC/Hibernate Transactions Duration?

Go To


I'm using jboss 4.2 with Hibernate. i need to log transactions duration, so I have statistics on the average time it takes a transaction to complete or rollback. another issue I have is jboss JTA (arjuna) aborting long running transactions, so I need to know what timeout to configure there.

2012-04-04 05:31
by erezul
What is your Hibernate version? Some versions of Hibernate can gather statistics themselves. Or you can use an AOP framework - Amir Pashazadeh 2012-04-04 06:03
Hibernate version is 3. - erezul 2012-04-08 06:10


I think you should create a timer that will count the time between the transaction start and end. A possibility to achieve this can be:

EntityManager em = HibernateUtil.createEntityManager(); //I used JPA's EntityManager instead of hibernate's sessions. createEntityManager iss a function created by you.
EntityTransaction tx = em.getTransaction();
Date startDate = new Date();

//... your DB stuff goes here ...

Date endDate = new Date();

MyLogger.logTransactionDuration(startDate, endDate);

In the above snippet of code, I make you the idea of how you can achieve this.

If you want, you can use log4j or slf4j api, and you would to make only some settings in file.

Hope to help.

2012-04-04 06:37
by artaxerxe
I am looking to a 'no coding' solution, something which is already supported by Hibernate or jboss JTA - erezul 2012-04-04 11:09