Skip to content

Commit ca69315

Browse files
committed
feat: 添加事务工具类,添加事务完成后执行回调的方法
1 parent 33505a6 commit ca69315

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

  • common/plugin/datasource/src/main/java/top/cadecode/uniboot/common/plugin/datasource/util
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package top.cadecode.uniboot.common.plugin.datasource.util;
2+
3+
import org.springframework.transaction.support.TransactionSynchronization;
4+
import org.springframework.transaction.support.TransactionSynchronizationManager;
5+
6+
/**
7+
* 事务工具类
8+
*
9+
* @author Cade Li
10+
* @since 2023/6/13
11+
*/
12+
public class TransactionUtil {
13+
14+
/**
15+
* 在当前事务完成后执行回调
16+
*
17+
* @param runnable 回调
18+
*/
19+
public static void doAfterCompletion(Runnable runnable) {
20+
// 若没有事务
21+
if (!TransactionSynchronizationManager.isActualTransactionActive()) {
22+
return;
23+
}
24+
TransactionSynchronizationManager.registerSynchronization(new DoTransactionCompletion(runnable));
25+
}
26+
27+
public static class DoTransactionCompletion implements TransactionSynchronization {
28+
29+
private final Runnable runnable;
30+
31+
public DoTransactionCompletion(Runnable runnable) {
32+
this.runnable = runnable;
33+
}
34+
35+
@Override
36+
public void afterCompletion(int status) {
37+
if (status != TransactionSynchronization.STATUS_COMMITTED) {
38+
return;
39+
}
40+
this.runnable.run();
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)