File tree Expand file tree Collapse file tree
common/plugin/datasource/src/main/java/top/cadecode/uniboot/common/plugin/datasource/util Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments