【问题标题】:Showing a Spring transaction in log在日志中显示 Spring 事务
【发布时间】:2010-12-30 05:50:23
【问题描述】:

我为 spring 配置了事务支持。有什么方法可以记录事务以确保我正确设置了所有内容?在日志中显示是查看正在发生的事情的好方法。

【问题讨论】:

    标签: java spring transactional


    【解决方案1】:

    在您的 log4j.properties 中(对于替代记录器或 log4j 的 xml 格式,请查看文档)

    根据您的事务管理器,您可以设置 Spring 框架的日志记录级别,以便它为您提供有关事务的更多信息。例如,如果使用JpaTransactionManager,则设置

    log4j.logger.org.springframework.orm.jpa=INFO
    

    (这是你的事务管理器的包),也是

    log4j.logger.org.springframework.transaction=INFO
    

    如果INFO 不够用,请使用DEBUG

    【讨论】:

    • INFO level 根本不会显示任何 tx 活动,这太冗长了。 DEBUG 在那里是必需的。
    • @Bozho 我有 JpaTransactionManager,我想监控何时从池中借用连接以及何时为特定事务释放连接。
    • 那么您需要更改连接池的日志记录配置
    • 如果我们使用mybatis+slf4j+logback+springboot呢?
    【解决方案2】:

    对我来说,一个不错的日志配置是:

    log4j.logger.org.springframework.transaction.interceptor = 跟踪

    它会像这样向我显示日志:

    2012-08-22 18:50:00,031 TRACE - 获取 [com.MyClass.myMethod] 的交易

    [我自己的来自方法 com.MyClass.myMethod 的日志语句]

    2012-08-22 18:50:00,142 TRACE - 完成 [com.MyClass.myMethod] 的交易

    【讨论】:

    • 太棒了!无需拥有其他包的所有信息/调试/跟踪日志记录,当这是您要查找的内容时:D
    【解决方案3】:

    对于带有 application.properties 的 Spring Boot 应用程序

    logging.level.ROOT=INFO
    logging.level.org.springframework.orm.jpa=DEBUG
    logging.level.org.springframework.transaction=DEBUG
    

    或者如果你更喜欢 Yaml (application.yaml)

    logging:
       level:
          org.springframework.orm.jpa: DEBUG
          org.springframework.transaction: DEBUG
    

    【讨论】:

      【解决方案4】:

      JtaTransactionManager.java 最有趣的日志信息(如果这个问题仍然是关于JtaTransactionManager)被记录在DEBUG 优先级。假设您在类路径中的某处有 log4j.properties,因此我建议使用:

      log4j.logger.org.springframework.transaction=DEBUG
      

      【讨论】:

        【解决方案5】:

        您也可以启用 JDBC 日志记录:

        log4j.logger.org.springframework.jdbc=DEBUG
        

        【讨论】:

          【解决方案6】:

          因为可以在运行时访问 Spring 类,所以可以确定事务状态。本文可能对您有所帮助:

          https://dzone.com/articles/monitoring-declarative-transac

          【讨论】:

          【解决方案7】:

          这是我在从ch.qos.logback.core.LayoutBase 派生的 Logback 布局实现中使用的一些代码。

          我创建了一个线程局部变量来存储对方法org.springframework.transaction.support.TransactionSynchronizationManager.isActualTransactionActive() 的引用。每当打印出新的日志行时,就会调用 getSpringTransactionInfo() 并返回一个单字符的字符串,该字符串将进入日志。

          参考资料:

          代码:

          private static ThreadLocal<Method> txCheckMethod;
          
          private static String getSpringTransactionInfo() {
              if (txCheckMethod == null) {
                  txCheckMethod = new ThreadLocal<Method>() {
                      @Override public Method initialValue() {           
                          try {
                              ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
                              Class<?> tsmClass = contextClassLoader.loadClass("org.springframework.transaction.support.TransactionSynchronizationManager");
                              return tsmClass.getMethod("isActualTransactionActive", (Class<?>[])null);
                          } catch (Exception e) {
                              e.printStackTrace();
                              return null;
                          }                      
                      }
                   };    
              }
              assert txCheckMethod != null;
              Method m = txCheckMethod.get();
              String res;
              if (m == null) {
                  res = " "; // there is no Spring here
              }
              else {
                  Boolean isActive = null;
                  try {
                      isActive = (Boolean) m.invoke((Object)null);
                      if (isActive) {
                          res = "T"; // transaction active                    
                      }
                      else {
                          res = "~"; // transaction inactive
                      }
                  }
                  catch (Exception exe) {
                      // suppress 
                      res = "?";
                  }
              }
              return res;
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-08-19
            • 2011-11-24
            • 1970-01-01
            • 2018-06-06
            • 2021-11-30
            • 1970-01-01
            • 2012-02-14
            • 1970-01-01
            相关资源
            最近更新 更多