Spring 事务结束后进行耗时操作
Spring Spring Boot 大约 2955 字TransactionSynchronizationManager
使用TransactionSynchronizationManager
类可以注册事务的事件监听,比如提交后、完成后等事件。
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
@Override
public void afterCommit() {
// 处理业务逻辑,比如:日志记录、耗时操作(耗时操作建议配合 @Async 异步处理)
System.out.println("afterCommit#" + Thread.currentThread().getId() + " " + Thread.currentThread().getName());
}
@Override
public void afterCompletion(int status) {
// int STATUS_COMMITTED = 0;
// int STATUS_ROLLED_BACK = 1;
// int STATUS_UNKNOWN = 2;
System.out.println("afterCompletion#" + status + ": " + Thread.currentThread().getId() + " " + Thread.currentThread().getName());
}
});
示例
@Transactional
public void tx1() {
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
@Override
public void afterCommit() {
// 处理业务逻辑,比如:日志记录、耗时操作(耗时操作建议配合 @Async 异步处理)
System.out.println("afterCommit#" + Thread.currentThread().getId() + " " + Thread.currentThread().getName());
}
@Override
public void afterCompletion(int status) {
// int STATUS_COMMITTED = 0;
// int STATUS_ROLLED_BACK = 1;
// int STATUS_UNKNOWN = 2;
System.out.println("afterCompletion#" + status + ": " + Thread.currentThread().getId() + " " + Thread.currentThread().getName());
}
});
System.out.println("111111111111#" + Thread.currentThread().getId() + " " + Thread.currentThread().getName());
// 模拟异常
int i = 1 / 0;
System.out.println("222222222222#" + Thread.currentThread().getId() + " " + Thread.currentThread().getName());
}
正常输出
111111111111#29 http-nio-8080-exec-1
222222222222#29 http-nio-8080-exec-1
afterCommit#29 http-nio-8080-exec-1
afterCompletion#0: 29 http-nio-8080-exec-1
异常输出
111111111111#29 http-nio-8080-exec-1
afterCompletion#1: 29 http-nio-8080-exec-1
@TransactionalEventListener
Spring
提供的@TransactionalEventListener
事务监听器。
示例
public class TxEvent extends ApplicationEvent {
public TxEvent(BizData source) {
super(source);
}
}
@Service
public class BizService {
@Resource
ApplicationEventPublisher applicationEventPublisher;
@Transactional
public void tx2() {
applicationEventPublisher.publishEvent(new TxEvent(new BizData(1, "张三")));
System.out.println("111111111111#" + Thread.currentThread().getId() + " " + Thread.currentThread().getName());
// int i = 1 / 0;
System.out.println("222222222222#" + Thread.currentThread().getId() + " " + Thread.currentThread().getName());
}
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
public void afterCommit(TxEvent event) {
System.out.println("afterCommit#" + event.getSource().toString() + " " + Thread.currentThread().getId() + " " + Thread.currentThread().getName());
}
}
阅读 783 · 发布于 2022-10-01
————        END        ————
Give me a Star, Thanks:)
https://github.com/fendoudebb扫描下方二维码关注公众号和小程序↓↓↓

昵称:
随便看看
换一批
-
23 种设计模式阅读 1859
-
记一次小程序排查 setData 函数耗时很长思路阅读 3647
-
GoLand 提示 Receiver has generic name阅读 2349
-
Linux -bash: locate: command not found阅读 999
-
Arthas 使用 tt 命令抓取调用异常、调用耗时、传入参数、返回参数阅读 3768
-
MySQL 查看当前时间阅读 1602
-
OLTP 与 OLAP阅读 3826
-
Spring 循环依赖能否用二级缓存解决阅读 2459
-
IDEA Kubernetes 远程 Debug 连接中断问题阅读 739
-
PostgreSQL 查看时区阅读 8262