Skip to content

Commit

Permalink
optimize: add tcc fence hook function (apache#6731)
Browse files Browse the repository at this point in the history
  • Loading branch information
chengliefeng committed Aug 19, 2024
1 parent 1c0a442 commit e1e4ff0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,17 @@


public interface FenceHandler {

default void beforePrepareFence(String xid, Long branchId, String actionName){}
Object prepareFence(String xid, Long branchId, String actionName, Callback<Object> targetCallback);
default void afterPrepareFence(String xid, Long branchId, String actionName){}

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

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

int deleteFenceByDate(Date datetime);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public static void setTransactionTemplate(TransactionTemplate transactionTemplat
*/
@Override
public Object prepareFence(String xid, Long branchId, String actionName, Callback<Object> targetCallback) {
executeWithHandling(() -> beforePrepareFence(xid, branchId, actionName), xid, branchId, "beforePrepareFence");
return transactionTemplate.execute(status -> {
try {
Connection conn = DataSourceUtils.getConnection(dataSource);
Expand All @@ -129,10 +130,27 @@ public Object prepareFence(String xid, Long branchId, String actionName, Callbac
} catch (Throwable t) {
status.setRollbackOnly();
throw new SkipCallbackWrapperException(t);
} finally {
executeWithHandling(() -> afterPrepareFence(xid, branchId, actionName), xid, branchId,"afterPrepareFence");
}
});
}

/**
* This method executes a Runnable with transaction context and logs any exceptions that occur without allowing them to propagate.
* @param runnable the runnable
* @param xid the global transaction id
* @param branchId the branch transaction id
* @param methodName the runnable name
*/
private void executeWithHandling(Runnable runnable, String xid, Long branchId, String methodName) {
try {
runnable.run();
} catch (Exception e) {
LOGGER.warn("Tcc fence Exception in {}, xid: {}, branchId: {}", methodName, xid, branchId, e);
}
}

/**
* common commit method enhanced
*
Expand All @@ -146,6 +164,7 @@ public Object prepareFence(String xid, Long branchId, String actionName, Callbac
@Override
public boolean commitFence(Method commitMethod, Object targetTCCBean,
String xid, Long branchId, Object[] args) {
executeWithHandling(() -> beforeCommitFence(xid, branchId), xid, branchId, "beforeCommitFence");
return transactionTemplate.execute(status -> {
try {
Connection conn = DataSourceUtils.getConnection(dataSource);
Expand All @@ -170,6 +189,8 @@ public boolean commitFence(Method commitMethod, Object targetTCCBean,
} catch (Throwable t) {
status.setRollbackOnly();
throw new SkipCallbackWrapperException(t);
} finally {
executeWithHandling(() -> afterCommitFence(xid, branchId), xid, branchId, "afterCommitFence");
}
});
}
Expand All @@ -188,6 +209,7 @@ public boolean commitFence(Method commitMethod, Object targetTCCBean,
@Override
public boolean rollbackFence(Method rollbackMethod, Object targetTCCBean,
String xid, Long branchId, Object[] args, String actionName) {
executeWithHandling(() -> beforeRollbackFence(xid, branchId, actionName), xid, branchId,"beforeRollbackFence");
return transactionTemplate.execute(status -> {
try {
Connection conn = DataSourceUtils.getConnection(dataSource);
Expand Down Expand Up @@ -219,6 +241,8 @@ public boolean rollbackFence(Method rollbackMethod, Object targetTCCBean,
} catch (Throwable t) {
status.setRollbackOnly();
throw new SkipCallbackWrapperException(t);
} finally {
executeWithHandling(() -> afterRollbackFence(xid, branchId, actionName), xid, branchId, "afterRollbackFence");
}
});
}
Expand Down

0 comments on commit e1e4ff0

Please sign in to comment.