Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear the transaction context in TCC prepare methed #1195

Merged
merged 5 commits into from
Jun 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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