Search code examples
javahibernatetransactionsglassfishlog4j

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


Solution

  • 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.