com*_*tta 93 java spring transactional
我为spring配置了事务支持.有没有办法记录事务只是为了确保我正确设置所有内容?在日志中显示是查看正在发生的事情的好方法.
Boz*_*zho 88
在你的log4j.properties(替代记录器,或log4j的xml格式,检查文档)
根据您的事务管理器,您可以设置spring框架的日志记录级别,以便它为您提供有关事务的更多信息.例如,在使用的情况下JpaTransactionManager,您设置
log4j.logger.org.springframework.orm.jpa=INFO
Run Code Online (Sandbox Code Playgroud)
(这是您的交易经理的包裹),也是
log4j.logger.org.springframework.transaction=INFO
Run Code Online (Sandbox Code Playgroud)
如果INFO还不够,请使用DEBUG
小智 60
对我来说,添加一个好的日志配置是:
log4j.logger.org.springframework.transaction.interceptor = trace
它会显示我这样的日志:
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]的交易
Mar*_*szS 22
对于Spring Boot应用程序:
logging.level.ROOT=INFO
logging.level.org.springframework.orm.jpa=DEBUG
logging.level.org.springframework.transaction=DEBUG
Run Code Online (Sandbox Code Playgroud)
最有趣的日志信息JtaTransactionManager.java(如果这个问题仍然是关于JtaTransactionManager)将被DEBUG优先记录.假设你log4j.properties在类路径上有某个地方,我建议使用:
log4j.logger.org.springframework.transaction=DEBUG
Run Code Online (Sandbox Code Playgroud)
小智 6
您还可以启用JDBC日志记录:
log4j.logger.org.springframework.jdbc=DEBUG
Run Code Online (Sandbox Code Playgroud)
以下是我在源自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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
79555 次 |
| 最近记录: |