use of io.seata.core.exception.TransactionException in project seata by seata.
the class ConnectionProxyTest method initBeforeEach.
@BeforeEach
public void initBeforeEach() throws Exception {
branchRollbackFlagField = ConnectionProxy.LockRetryPolicy.class.getDeclaredField("LOCK_RETRY_POLICY_BRANCH_ROLLBACK_ON_CONFLICT");
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(branchRollbackFlagField, branchRollbackFlagField.getModifiers() & ~Modifier.FINAL);
branchRollbackFlagField.setAccessible(true);
boolean branchRollbackFlag = (boolean) branchRollbackFlagField.get(null);
Assertions.assertTrue(branchRollbackFlag);
dataSourceProxy = Mockito.mock(DataSourceProxy.class);
Mockito.when(dataSourceProxy.getResourceId()).thenReturn(TEST_RESOURCE_ID);
ResourceManager rm = Mockito.mock(ResourceManager.class);
Mockito.when(rm.branchRegister(BranchType.AT, dataSourceProxy.getResourceId(), null, TEST_XID, null, lockKey)).thenThrow(new TransactionException(TransactionExceptionCode.LockKeyConflict));
DefaultResourceManager defaultResourceManager = DefaultResourceManager.get();
Assertions.assertNotNull(defaultResourceManager);
DefaultResourceManager.mockResourceManager(BranchType.AT, rm);
}
use of io.seata.core.exception.TransactionException in project seata by seata.
the class ConnectionProxyXA method setAutoCommit.
@Override
public void setAutoCommit(boolean autoCommit) throws SQLException {
if (currentAutoCommitStatus == autoCommit) {
return;
}
if (autoCommit) {
// auto-commit mode is changed, the transaction is committed.
if (xaActive) {
commit();
}
} else {
if (xaActive) {
throw new SQLException("should NEVER happen: setAutoCommit from true to false while xa branch is active");
}
// Start a XA branch
long branchId = 0L;
try {
// 1. register branch to TC then get the branchId
branchId = DefaultResourceManager.get().branchRegister(BranchType.XA, resource.getResourceId(), null, xid, null, null);
} catch (TransactionException te) {
cleanXABranchContext();
throw new SQLException("failed to register xa branch " + xid + " since " + te.getCode() + ":" + te.getMessage(), te);
}
// 2. build XA-Xid with xid and branchId
this.xaBranchXid = XAXidBuilder.build(xid, branchId);
try {
// 3. XA Start
xaResource.start(this.xaBranchXid, XAResource.TMNOFLAGS);
} catch (XAException e) {
cleanXABranchContext();
throw new SQLException("failed to start xa branch " + xid + " since " + e.getMessage(), e);
}
// 4. XA is active
this.xaActive = true;
}
currentAutoCommitStatus = autoCommit;
}
use of io.seata.core.exception.TransactionException in project seata by seata.
the class ConnectionProxyXA method commit.
@Override
public void commit() throws SQLException {
if (currentAutoCommitStatus) {
// Ignore the committing on an autocommit session.
return;
}
if (!xaActive || this.xaBranchXid == null) {
throw new SQLException("should NOT commit on an inactive session", SQLSTATE_XA_NOT_END);
}
try {
// XA End: Success
xaResource.end(xaBranchXid, XAResource.TMSUCCESS);
// XA Prepare
xaResource.prepare(xaBranchXid);
// Keep the Connection if necessary
keepIfNecessary();
} catch (XAException xe) {
try {
// Branch Report to TC: Failed
DefaultResourceManager.get().branchReport(BranchType.XA, xid, xaBranchXid.getBranchId(), BranchStatus.PhaseOne_Failed, null);
} catch (TransactionException te) {
LOGGER.warn("Failed to report XA branch commit-failure on " + xid + "-" + xaBranchXid.getBranchId() + " since " + te.getCode() + ":" + te.getMessage() + " and XAException:" + xe.getMessage());
}
throw new SQLException("Failed to end(TMSUCCESS)/prepare xa branch on " + xid + "-" + xaBranchXid.getBranchId() + " since " + xe.getMessage(), xe);
} finally {
cleanXABranchContext();
}
}
use of io.seata.core.exception.TransactionException in project seata by seata.
the class ConnectionProxyXA method rollback.
@Override
public void rollback() throws SQLException {
if (currentAutoCommitStatus) {
// Ignore the committing on an autocommit session.
return;
}
if (!xaActive || this.xaBranchXid == null) {
throw new SQLException("should NOT rollback on an inactive session");
}
try {
// XA End: Fail
xaResource.end(xaBranchXid, XAResource.TMFAIL);
xaRollback(xaBranchXid);
// Branch Report to TC
DefaultResourceManager.get().branchReport(BranchType.XA, xid, xaBranchXid.getBranchId(), BranchStatus.PhaseOne_Failed, null);
LOGGER.info(xaBranchXid + " was rollbacked");
} catch (XAException xe) {
throw new SQLException("Failed to end(TMFAIL) xa branch on " + xid + "-" + xaBranchXid.getBranchId() + " since " + xe.getMessage(), xe);
} catch (TransactionException te) {
// log and ignore the report failure
LOGGER.warn("Failed to report XA branch rollback on " + xid + "-" + xaBranchXid.getBranchId() + " since " + te.getCode() + ":" + te.getMessage());
} finally {
cleanXABranchContext();
}
}
use of io.seata.core.exception.TransactionException in project seata by seata.
the class AbstractTCInboundHandler method handle.
@Override
public GlobalReportResponse handle(GlobalReportRequest request, final RpcContext rpcContext) {
GlobalReportResponse response = new GlobalReportResponse();
response.setGlobalStatus(request.getGlobalStatus());
exceptionHandleTemplate(new AbstractCallback<GlobalReportRequest, GlobalReportResponse>() {
@Override
public void execute(GlobalReportRequest request, GlobalReportResponse response) throws TransactionException {
doGlobalReport(request, response, rpcContext);
}
}, request, response);
return response;
}
Aggregations