java 如何在 Hibernate 中记录 DB 事务的开始和完成

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30576385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 17:20:06  来源:igfitidea点击:

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

javahibernatetransactionsglassfishlog4j

提问by Bhuvan

sql_show = true

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

hibernate 中的此属性打印正在运行的 sql,但我也想查看begin transactionandcomplete 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.

此类具有 begin 和 complete 方法,但此方法不记录任何内容。

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

任何建议如何在休眠中查看事务级别信息
我正在使用休眠 2.2

回答by Vlad Mihalcea

For Hibernate 5

对于休眠 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>
    
  • 对于 SLF4J 日志记录:

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

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

For Hibernate 4

对于休眠 4

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

您需要将以下类的日志记录阈值设置为 DEBUG:

  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>
      
  1. 对于 JDBC 事务(例如 RESOURCE_LOCAL)

    • 对于 SLF4J 日志记录:

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

      <logger name="org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction">
         <level value="DEBUG"/>
      </logger>
      
  2. 对于 JTA 事务

    • 对于 SLF4J 日志记录:

      <logger name="org.hibernate.engine.transaction.internal.jta.JtaTransaction" level="debug"/>
      
    • 对于 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.

最好为尽可能少的类激活 DEBUG 级别,否则,您的日志大小将急剧增加。

回答by laughter

try to set hibernate generate_statistics property

尝试设置休眠 generate_statistics 属性

<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

然后你会看到所有的hibernate日志,你就可以正确选择hibernate类来登录log4j配置文件

回答by Alexander M

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

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