Search in sources :

Example 21 with TransactionException

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);
}
Also used : Field(java.lang.reflect.Field) TransactionException(io.seata.core.exception.TransactionException) DefaultResourceManager(io.seata.rm.DefaultResourceManager) DefaultResourceManager(io.seata.rm.DefaultResourceManager) ResourceManager(io.seata.core.model.ResourceManager) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 22 with TransactionException

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

Example 23 with TransactionException

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

Example 24 with TransactionException

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

Example 25 with TransactionException

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;
}
Also used : TransactionException(io.seata.core.exception.TransactionException) GlobalReportRequest(io.seata.core.protocol.transaction.GlobalReportRequest) GlobalReportResponse(io.seata.core.protocol.transaction.GlobalReportResponse)

Aggregations

TransactionException (io.seata.core.exception.TransactionException)54 BranchStatus (io.seata.core.model.BranchStatus)14 GlobalSession (io.seata.server.session.GlobalSession)9 ShouldNeverHappenException (io.seata.common.exception.ShouldNeverHappenException)7 StoreException (io.seata.common.exception.StoreException)7 ExecutionException (io.seata.tm.api.TransactionalExecutor.ExecutionException)6 GlobalTransaction (io.seata.tm.api.GlobalTransaction)5 IOException (java.io.IOException)5 TimeoutException (java.util.concurrent.TimeoutException)5 NotSupportYetException (io.seata.common.exception.NotSupportYetException)4 GlobalTransactionEvent (io.seata.core.event.GlobalTransactionEvent)4 GlobalTransactionException (io.seata.core.exception.GlobalTransactionException)4 GlobalStatus (io.seata.core.model.GlobalStatus)4 EngineExecutionException (io.seata.saga.engine.exception.EngineExecutionException)4 BranchSession (io.seata.server.session.BranchSession)4 SQLException (java.sql.SQLException)4 StateMachineInstance (io.seata.saga.statelang.domain.StateMachineInstance)3 TransactionalExecutor (io.seata.tm.api.TransactionalExecutor)3 XAException (javax.transaction.xa.XAException)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3