use of io.seata.core.model.Resource in project seata by seata.
the class XAModeTest2 method initRM.
private void initRM() throws Throwable {
// init RM
DefaultResourceManager.get();
// mock the RM of XA
DefaultResourceManager.mockResourceManager(BranchType.XA, new ResourceManagerXA() {
@Override
public void registerResource(Resource resource) {
dataSourceCache.put(resource.getResourceId(), resource);
}
@Override
public Long branchRegister(BranchType branchType, String resourceId, String clientId, String xid, String applicationData, String lockKeys) throws TransactionException {
return mockBranchId;
}
@Override
public void branchReport(BranchType branchType, String xid, long branchId, BranchStatus status, String applicationData) throws TransactionException {
}
});
}
use of io.seata.core.model.Resource in project seata by seata.
the class ResourceManagerXA method finishBranch.
private BranchStatus finishBranch(boolean committed, BranchType branchType, String xid, long branchId, String resourceId, String applicationData) throws TransactionException {
XAXid xaBranchXid = XAXidBuilder.build(xid, branchId);
Resource resource = dataSourceCache.get(resourceId);
if (resource instanceof AbstractDataSourceProxyXA) {
try (ConnectionProxyXA connectionProxyXA = ((AbstractDataSourceProxyXA) resource).getConnectionForXAFinish(xaBranchXid)) {
if (committed) {
connectionProxyXA.xaCommit(xid, branchId, applicationData);
LOGGER.info(xaBranchXid + " was committed.");
return BranchStatus.PhaseTwo_Committed;
} else {
connectionProxyXA.xaRollback(xid, branchId, applicationData);
LOGGER.info(xaBranchXid + " was rollbacked");
return BranchStatus.PhaseTwo_Rollbacked;
}
} catch (XAException | SQLException sqle) {
if (sqle instanceof XAException) {
if (((XAException) sqle).errorCode == XAException.XAER_NOTA) {
if (committed) {
return BranchStatus.PhaseTwo_Committed;
} else {
return BranchStatus.PhaseTwo_Rollbacked;
}
}
}
if (committed) {
LOGGER.info(xaBranchXid + " commit failed since " + sqle.getMessage(), sqle);
// FIXME: case of PhaseTwo_CommitFailed_Unretryable
return BranchStatus.PhaseTwo_CommitFailed_Retryable;
} else {
LOGGER.info(xaBranchXid + " rollback failed since " + sqle.getMessage(), sqle);
// FIXME: case of PhaseTwo_RollbackFailed_Unretryable
return BranchStatus.PhaseTwo_RollbackFailed_Retryable;
}
}
} else {
LOGGER.error("Unknown Resource for XA resource " + resourceId + " " + resource);
if (committed) {
return BranchStatus.PhaseTwo_CommitFailed_Unretryable;
} else {
return BranchStatus.PhaseTwo_RollbackFailed_Unretryable;
}
}
}
Aggregations