Search in sources :

Example 1 with Resource

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 {
        }
    });
}
Also used : TransactionException(io.seata.core.exception.TransactionException) BranchType(io.seata.core.model.BranchType) ResourceManagerXA(io.seata.rm.datasource.xa.ResourceManagerXA) XAResource(javax.transaction.xa.XAResource) Resource(io.seata.core.model.Resource) BranchStatus(io.seata.core.model.BranchStatus)

Example 2 with Resource

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;
        }
    }
}
Also used : XAException(javax.transaction.xa.XAException) SQLException(java.sql.SQLException) Resource(io.seata.core.model.Resource)

Aggregations

Resource (io.seata.core.model.Resource)2 TransactionException (io.seata.core.exception.TransactionException)1 BranchStatus (io.seata.core.model.BranchStatus)1 BranchType (io.seata.core.model.BranchType)1 ResourceManagerXA (io.seata.rm.datasource.xa.ResourceManagerXA)1 SQLException (java.sql.SQLException)1 XAException (javax.transaction.xa.XAException)1 XAResource (javax.transaction.xa.XAResource)1