Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log the start and the completion of DB transactions in Hibernate

sql_show = true

this property in hibernate prints the sql that is run, but i want to see the begin transaction and complete transaction statements as well so that i can track the transaction duration and see the query run in which transaction.

googling reveals that

log4j.logger.org.hibernate.SQL = DEBUG, defaultAppender
log4j.logger.org.hibernate.type = DEBUG, defaultAppender
log4j.logger.org.hibernate.transaction=DEBUG, defaultAppender

should show you the transaction level data as well. But it doesnt.

Investigating more i looked into hibernate code and found a class name

org.hibernate.ejb.TransactionImpl

this class has the begin and complete method but this method does not log any thing.

Any advice how to see the transaction level info in hibernate ?
I am using hibernate 2.2

like image 545
Bhuvan Avatar asked Jun 01 '15 14:06

Bhuvan


People also ask

How do I enable hibernate logging in SQL?

The better way to activate the logging of executed SQL statements is to set the log level of the org. hibernate. SQL category to DEBUG (or the corresponding log level of your log framework).

What is logging in hibernate?

Hibernate utilizes Simple Logging Facade for Java (SLF4J) in order to log various system events. SLF4J can direct your logging output to several logging frameworks (NOP, Simple, log4j version 1.2, JDK 1.4 logging, JCL or logback) depending on your chosen binding.


1 Answers

For Hibernate 5

  • For SLF4J logging:

    <logger name="org.hibernate.engine.transaction.internal.TransactionImpl" level="debug"/>
    
  • For Log4j:

     <logger name="org.hibernate.engine.transaction.internal.TransactionImpl">
          <level value="DEBUG"/>
     </logger>
    

For Hibernate 4

You need to set the logging threshold to DEBUG for the following classes:

  1. For JDBC transactions (e.g. RESOURCE_LOCAL)

    • For SLF4J logging:

      <logger name="org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction" level="debug"/>
      
    • For Log4j:

      <logger name="org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction">
         <level value="DEBUG"/>
      </logger>
      
  2. For JTA transactions

    • For SLF4J logging:

      <logger name="org.hibernate.engine.transaction.internal.jta.JtaTransaction" level="debug"/>
      
    • For Log4j:

      <logger name="org.hibernate.engine.transaction.internal.jta.JtaTransaction">
         <level value="DEBUG"/>
      </logger>
      

It's better to activate the DEBUG level for as few classes as possible because otherwise, your logs size will increase dramatically.

like image 87
Vlad Mihalcea Avatar answered Sep 20 '22 21:09

Vlad Mihalcea