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

19,220

Solution 1

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.

Solution 2

try to set hibernate generate_statistics property

<prop key="hibernate.generate_statistics">true</prop>

and set

log4j.logger.org.hibernate=DEBUG

then you will see all hibernate logs, and you will be able to correctly choose hibernate classes to log in log4j configuration file

Solution 3

If you also want to see transaction isolation level for new created transactions:

log4j.logger.org.springframework.transaction.support.AbstractPlatformTransactionManager=debug log4j.logger.org.springframework.orm.hibernate5.HibernateTransactionManager=debug log4j.logger.org.springframework.orm.jpa.JpaTransactionManager=debug log4j.logger.org.springframework.jdbc.datasource.DataSourceTransactionManager=debug

Solution 4

Enabling these logs may be helpful also

<logger name="org.hibernate.resource.transaction" level="debug"/>
<logger name="org.hibernate.resource.jdbc" level="debug"/>
<logger name="org.hibernate.internal.SessionImpl" level="debug"/>
<logger name="org.hibernate.internal.SessionFactoryImpl" level="debug"/>

(hibernate 5)

it prints logs about session begin/close and other relevant details

[org.hibernate.internal.SessionImpl] (default task-19) Opened Session [8c3ecbac-91b5-4dd8-b012-bfb1b4fe476c] at timestamp: 1592951456170 .....

[org.hibernate.internal.SessionImpl] (default task-19) Closing session [8c3ecbac-91b5-4dd8-b012-bfb1b4fe476c] ........ [org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl] (default task-19) Logical connection closed

Share:
19,220

Related videos on Youtube

Bhuvan
Author by

Bhuvan

Updated on September 15, 2022

Comments

  • Bhuvan
    Bhuvan about 1 year
    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

  • davidfrancis
    davidfrancis about 5 years
    The above logger levels didn't work for me What worked was: org.hibernate.engine.transaction.internal.TransactionImpl
  • Vlad Mihalcea
    Vlad Mihalcea about 5 years
    Thanks. That was written for Hibernate 4 but I updated it for 5 as well.