Skip to content

Commit e1e4ff0

Browse files
author
chengliefeng
committed
optimize: add tcc fence hook function (apache#6731)
1 parent 1c0a442 commit e1e4ff0

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

integration-tx-api/src/main/java/org/apache/seata/integration/tx/api/fence/FenceHandler.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,17 @@
2323

2424

2525
public interface FenceHandler {
26-
26+
default void beforePrepareFence(String xid, Long branchId, String actionName){}
2727
Object prepareFence(String xid, Long branchId, String actionName, Callback<Object> targetCallback);
28+
default void afterPrepareFence(String xid, Long branchId, String actionName){}
2829

30+
default void beforeCommitFence(String xid, Long branchId){}
2931
boolean commitFence(Method commitMethod, Object targetTCCBean, String xid, Long branchId, Object[] args);
32+
default void afterCommitFence(String xid, Long branchId){}
3033

34+
default void beforeRollbackFence(String xid, Long branchId, String actionName){}
3135
boolean rollbackFence(Method rollbackMethod, Object targetTCCBean, String xid, Long branchId, Object[] args, String actionName);
36+
default void afterRollbackFence(String xid, Long branchId, String actionName){}
3237

3338
int deleteFenceByDate(Date datetime);
3439

spring/src/main/java/org/apache/seata/rm/fence/SpringFenceHandler.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ public static void setTransactionTemplate(TransactionTemplate transactionTemplat
108108
*/
109109
@Override
110110
public Object prepareFence(String xid, Long branchId, String actionName, Callback<Object> targetCallback) {
111+
executeWithHandling(() -> beforePrepareFence(xid, branchId, actionName), xid, branchId, "beforePrepareFence");
111112
return transactionTemplate.execute(status -> {
112113
try {
113114
Connection conn = DataSourceUtils.getConnection(dataSource);
@@ -129,10 +130,27 @@ public Object prepareFence(String xid, Long branchId, String actionName, Callbac
129130
} catch (Throwable t) {
130131
status.setRollbackOnly();
131132
throw new SkipCallbackWrapperException(t);
133+
} finally {
134+
executeWithHandling(() -> afterPrepareFence(xid, branchId, actionName), xid, branchId,"afterPrepareFence");
132135
}
133136
});
134137
}
135138

139+
/**
140+
* This method executes a Runnable with transaction context and logs any exceptions that occur without allowing them to propagate.
141+
* @param runnable the runnable
142+
* @param xid the global transaction id
143+
* @param branchId the branch transaction id
144+
* @param methodName the runnable name
145+
*/
146+
private void executeWithHandling(Runnable runnable, String xid, Long branchId, String methodName) {
147+
try {
148+
runnable.run();
149+
} catch (Exception e) {
150+
LOGGER.warn("Tcc fence Exception in {}, xid: {}, branchId: {}", methodName, xid, branchId, e);
151+
}
152+
}
153+
136154
/**
137155
* common commit method enhanced
138156
*
@@ -146,6 +164,7 @@ public Object prepareFence(String xid, Long branchId, String actionName, Callbac
146164
@Override
147165
public boolean commitFence(Method commitMethod, Object targetTCCBean,
148166
String xid, Long branchId, Object[] args) {
167+
executeWithHandling(() -> beforeCommitFence(xid, branchId), xid, branchId, "beforeCommitFence");
149168
return transactionTemplate.execute(status -> {
150169
try {
151170
Connection conn = DataSourceUtils.getConnection(dataSource);
@@ -170,6 +189,8 @@ public boolean commitFence(Method commitMethod, Object targetTCCBean,
170189
} catch (Throwable t) {
171190
status.setRollbackOnly();
172191
throw new SkipCallbackWrapperException(t);
192+
} finally {
193+
executeWithHandling(() -> afterCommitFence(xid, branchId), xid, branchId, "afterCommitFence");
173194
}
174195
});
175196
}
@@ -188,6 +209,7 @@ public boolean commitFence(Method commitMethod, Object targetTCCBean,
188209
@Override
189210
public boolean rollbackFence(Method rollbackMethod, Object targetTCCBean,
190211
String xid, Long branchId, Object[] args, String actionName) {
212+
executeWithHandling(() -> beforeRollbackFence(xid, branchId, actionName), xid, branchId,"beforeRollbackFence");
191213
return transactionTemplate.execute(status -> {
192214
try {
193215
Connection conn = DataSourceUtils.getConnection(dataSource);
@@ -219,6 +241,8 @@ public boolean rollbackFence(Method rollbackMethod, Object targetTCCBean,
219241
} catch (Throwable t) {
220242
status.setRollbackOnly();
221243
throw new SkipCallbackWrapperException(t);
244+
} finally {
245+
executeWithHandling(() -> afterRollbackFence(xid, branchId, actionName), xid, branchId, "afterRollbackFence");
222246
}
223247
});
224248
}

0 commit comments

Comments
 (0)