Skip to content

Commit

Permalink
Clear the transaction context in TCC prepare methed (#1195)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangthen authored and ujjboy committed Jun 13, 2019
1 parent 0071943 commit 442e048
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
Expand Up @@ -33,6 +33,7 @@
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.io.IOException;
import java.util.Collection;
import java.util.stream.Stream;

Expand Down Expand Up @@ -64,13 +65,21 @@ public class DefaultCoreTest {

private static final String applicationData = "{\"data\":\"test\"}";

static {
try {
initSessionManager();
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* Init session manager.
*
* @throws Exception the exception
*/
@BeforeEach
public void initSessionManager() throws Exception {
// @BeforeEach
public static void initSessionManager() throws Exception {
SessionHolder.init(null);
}

Expand All @@ -88,6 +97,7 @@ public void branchRegisterTest(String xid) throws Exception {
GlobalSession globalSession = SessionHolder.findGlobalSession(xid);
Assertions.assertEquals(globalSession.getSortedBranches().size(), 1);


//clear
globalSession.end();
}
Expand Down
37 changes: 23 additions & 14 deletions spring/src/main/java/io/seata/spring/tcc/TccActionInterceptor.java
Expand Up @@ -65,25 +65,34 @@ public TccActionInterceptor(RemotingDesc remotingDesc) {

@Override
public Object invoke(final MethodInvocation invocation) throws Throwable {
if(!RootContext.inGlobalTransaction()){
//not in transaction
return invocation.proceed();
}
Method method = getActionInterfaceMethod(invocation);
TwoPhaseBusinessAction businessAction = method.getAnnotation(TwoPhaseBusinessAction.class);
//try method
if (businessAction != null) {
if (StringUtils.isBlank(RootContext.getXID())) {
//not in distribute transaction
return invocation.proceed();
//save the xid
String xid = RootContext.getXID();
//clear the context
RootContext.unbind();
try {
Object[] methodArgs = invocation.getArguments();
//Handler the TCC Aspect
Map<String, Object> ret = actionInterceptorHandler.proceed(method, methodArgs, businessAction,
new Callback<Object>() {
@Override
public Object execute() throws Throwable {
return invocation.proceed();
}
});
//return the final result
return ret.get(Constants.TCC_METHOD_RESULT);
} finally {
//recovery the context
RootContext.bind(xid);
}
Object[] methodArgs = invocation.getArguments();
//Handler the TCC Aspect
Map<String, Object> ret = actionInterceptorHandler.proceed(method, methodArgs, businessAction,
new Callback<Object>() {
@Override
public Object execute() throws Throwable {
return invocation.proceed();
}
});
//return the final result
return ret.get(Constants.TCC_METHOD_RESULT);
}
return invocation.proceed();
}
Expand Down

0 comments on commit 442e048

Please sign in to comment.