Search in sources :

Example 21 with StoreException

use of io.seata.common.exception.StoreException in project XHuiCloud by sindaZeng.

the class SessionHolder method init.

/**
 * Init.
 *
 * @param mode the store mode: file, db
 * @throws IOException the io exception
 */
public static void init(String mode) {
    if (StringUtils.isBlank(mode)) {
        mode = CONFIG.getConfig(ConfigurationKeys.STORE_MODE);
    }
    StoreMode storeMode = StoreMode.get(mode);
    if (StoreMode.DB.equals(storeMode)) {
        ROOT_SESSION_MANAGER = EnhancedServiceLoader.load(SessionManager.class, StoreMode.DB.getName());
        ASYNC_COMMITTING_SESSION_MANAGER = EnhancedServiceLoader.load(SessionManager.class, StoreMode.DB.getName(), new Object[] { ASYNC_COMMITTING_SESSION_MANAGER_NAME });
        RETRY_COMMITTING_SESSION_MANAGER = EnhancedServiceLoader.load(SessionManager.class, StoreMode.DB.getName(), new Object[] { RETRY_COMMITTING_SESSION_MANAGER_NAME });
        RETRY_ROLLBACKING_SESSION_MANAGER = EnhancedServiceLoader.load(SessionManager.class, StoreMode.DB.getName(), new Object[] { RETRY_ROLLBACKING_SESSION_MANAGER_NAME });
    } else if (StoreMode.FILE.equals(storeMode)) {
        String sessionStorePath = CONFIG.getConfig(ConfigurationKeys.STORE_FILE_DIR, DEFAULT_SESSION_STORE_FILE_DIR);
        if (StringUtils.isBlank(sessionStorePath)) {
            throw new StoreException("the {store.file.dir} is empty.");
        }
        ROOT_SESSION_MANAGER = EnhancedServiceLoader.load(SessionManager.class, StoreMode.FILE.getName(), new Object[] { ROOT_SESSION_MANAGER_NAME, sessionStorePath });
        ASYNC_COMMITTING_SESSION_MANAGER = EnhancedServiceLoader.load(SessionManager.class, StoreMode.FILE.getName(), new Class[] { String.class, String.class }, new Object[] { ASYNC_COMMITTING_SESSION_MANAGER_NAME, null });
        RETRY_COMMITTING_SESSION_MANAGER = EnhancedServiceLoader.load(SessionManager.class, StoreMode.FILE.getName(), new Class[] { String.class, String.class }, new Object[] { RETRY_COMMITTING_SESSION_MANAGER_NAME, null });
        RETRY_ROLLBACKING_SESSION_MANAGER = EnhancedServiceLoader.load(SessionManager.class, StoreMode.FILE.getName(), new Class[] { String.class, String.class }, new Object[] { RETRY_ROLLBACKING_SESSION_MANAGER_NAME, null });
    } else if (StoreMode.REDIS.equals(storeMode)) {
        ROOT_SESSION_MANAGER = EnhancedServiceLoader.load(SessionManager.class, StoreMode.REDIS.getName());
        ASYNC_COMMITTING_SESSION_MANAGER = EnhancedServiceLoader.load(SessionManager.class, StoreMode.REDIS.getName(), new Object[] { ASYNC_COMMITTING_SESSION_MANAGER_NAME });
        RETRY_COMMITTING_SESSION_MANAGER = EnhancedServiceLoader.load(SessionManager.class, StoreMode.REDIS.getName(), new Object[] { RETRY_COMMITTING_SESSION_MANAGER_NAME });
        RETRY_ROLLBACKING_SESSION_MANAGER = EnhancedServiceLoader.load(SessionManager.class, StoreMode.REDIS.getName(), new Object[] { RETRY_ROLLBACKING_SESSION_MANAGER_NAME });
    } else {
        // unknown store
        throw new IllegalArgumentException("unknown store mode:" + mode);
    }
    reload(storeMode);
}
Also used : StoreMode(io.seata.core.store.StoreMode) StoreException(io.seata.common.exception.StoreException)

Example 22 with StoreException

use of io.seata.common.exception.StoreException in project XHuiCloud by sindaZeng.

the class RedisTransactionStoreManager method updateGlobalTransactionDO.

/**
 * Update the global transaction.
 * It will update two parts:
 *  1.the global session map
 *  2.the global status list
 * If the update failed,the succeed operates will rollback
 * @param globalTransactionDO
 * @return
 */
private boolean updateGlobalTransactionDO(GlobalTransactionDO globalTransactionDO) {
    String xid = globalTransactionDO.getXid();
    String globalKey = buildGlobalKeyByTransactionId(globalTransactionDO.getTransactionId());
    try (Jedis jedis = JedisPooledFactory.getJedisInstance()) {
        // Defensive watch to prevent other TC server operating concurrently,Fail fast
        jedis.watch(globalKey);
        List<String> statusAndGmtModified = jedis.hmget(globalKey, REDIS_KEY_GLOBAL_STATUS, REDIS_KEY_GLOBAL_GMT_MODIFIED);
        String previousStatus = statusAndGmtModified.get(0);
        if (StringUtils.isEmpty(previousStatus)) {
            jedis.unwatch();
            throw new StoreException("Global transaction is not exist, update global transaction failed.");
        }
        if (previousStatus.equals(String.valueOf(globalTransactionDO.getStatus()))) {
            jedis.unwatch();
            return true;
        }
        String previousGmtModified = statusAndGmtModified.get(1);
        Transaction multi = jedis.multi();
        Map<String, String> map = new HashMap<>(2);
        map.put(REDIS_KEY_GLOBAL_STATUS, String.valueOf(globalTransactionDO.getStatus()));
        map.put(REDIS_KEY_GLOBAL_GMT_MODIFIED, String.valueOf((new Date()).getTime()));
        multi.hmset(globalKey, map);
        multi.lrem(buildGlobalStatus(Integer.valueOf(previousStatus)), 0, xid);
        multi.rpush(buildGlobalStatus(globalTransactionDO.getStatus()), xid);
        List<Object> exec = multi.exec();
        String hmset = exec.get(0).toString();
        long lrem = (long) exec.get(1);
        long rpush = (long) exec.get(2);
        if (OK.equalsIgnoreCase(hmset) && lrem > 0 && rpush > 0) {
            return true;
        } else {
            // If someone failed, the succeed operations need rollback
            if (OK.equalsIgnoreCase(hmset)) {
                // Defensive watch to prevent other TC server operating concurrently,give up this operate
                jedis.watch(globalKey);
                String xid2 = jedis.hget(globalKey, REDIS_KEY_GLOBAL_XID);
                if (StringUtils.isNotEmpty(xid2)) {
                    Map<String, String> mapPrevious = new HashMap<>(2, 1);
                    mapPrevious.put(REDIS_KEY_GLOBAL_STATUS, previousStatus);
                    mapPrevious.put(REDIS_KEY_GLOBAL_GMT_MODIFIED, previousGmtModified);
                    Transaction multi2 = jedis.multi();
                    multi2.hmset(globalKey, mapPrevious);
                    multi2.exec();
                }
            }
            if (lrem > 0) {
                jedis.rpush(buildGlobalStatus(Integer.valueOf(previousStatus)), xid);
            }
            if (rpush > 0) {
                jedis.lrem(buildGlobalStatus(globalTransactionDO.getStatus()), 0, xid);
            }
            return false;
        }
    } catch (Exception ex) {
        throw new RedisException(ex);
    }
}
Also used : StoreException(io.seata.common.exception.StoreException) RedisException(io.seata.common.exception.RedisException) StoreException(io.seata.common.exception.StoreException) Jedis(redis.clients.jedis.Jedis) Transaction(redis.clients.jedis.Transaction) RedisException(io.seata.common.exception.RedisException)

Example 23 with StoreException

use of io.seata.common.exception.StoreException in project seata by seata.

the class AbstractTCInboundHandler method handle.

@Override
public GlobalBeginResponse handle(GlobalBeginRequest request, final RpcContext rpcContext) {
    GlobalBeginResponse response = new GlobalBeginResponse();
    exceptionHandleTemplate(new AbstractCallback<GlobalBeginRequest, GlobalBeginResponse>() {

        @Override
        public void execute(GlobalBeginRequest request, GlobalBeginResponse response) throws TransactionException {
            try {
                doGlobalBegin(request, response, rpcContext);
            } catch (StoreException e) {
                throw new TransactionException(TransactionExceptionCode.FailedStore, String.format("begin global request failed. xid=%s, msg=%s", response.getXid(), e.getMessage()), e);
            }
        }
    }, request, response);
    return response;
}
Also used : GlobalBeginRequest(io.seata.core.protocol.transaction.GlobalBeginRequest) TransactionException(io.seata.core.exception.TransactionException) GlobalBeginResponse(io.seata.core.protocol.transaction.GlobalBeginResponse) StoreException(io.seata.common.exception.StoreException)

Example 24 with StoreException

use of io.seata.common.exception.StoreException in project seata by seata.

the class AbstractTCInboundHandler method handle.

@Override
public GlobalStatusResponse handle(GlobalStatusRequest request, final RpcContext rpcContext) {
    GlobalStatusResponse response = new GlobalStatusResponse();
    response.setGlobalStatus(GlobalStatus.UnKnown);
    exceptionHandleTemplate(new AbstractCallback<GlobalStatusRequest, GlobalStatusResponse>() {

        @Override
        public void execute(GlobalStatusRequest request, GlobalStatusResponse response) throws TransactionException {
            try {
                doGlobalStatus(request, response, rpcContext);
            } catch (StoreException e) {
                throw new TransactionException(TransactionExceptionCode.FailedStore, String.format("global status request failed. xid=%s, msg=%s", request.getXid(), e.getMessage()), e);
            }
        }

        @Override
        public void onTransactionException(GlobalStatusRequest request, GlobalStatusResponse response, TransactionException tex) {
            super.onTransactionException(request, response, tex);
            checkTransactionStatus(request, response);
        }

        @Override
        public void onException(GlobalStatusRequest request, GlobalStatusResponse response, Exception rex) {
            super.onException(request, response, rex);
            checkTransactionStatus(request, response);
        }
    }, request, response);
    return response;
}
Also used : TransactionException(io.seata.core.exception.TransactionException) GlobalStatusRequest(io.seata.core.protocol.transaction.GlobalStatusRequest) TransactionException(io.seata.core.exception.TransactionException) StoreException(io.seata.common.exception.StoreException) GlobalStatusResponse(io.seata.core.protocol.transaction.GlobalStatusResponse) StoreException(io.seata.common.exception.StoreException)

Example 25 with StoreException

use of io.seata.common.exception.StoreException in project seata by seata.

the class AbstractTCInboundHandler method handle.

@Override
public BranchRegisterResponse handle(BranchRegisterRequest request, final RpcContext rpcContext) {
    BranchRegisterResponse response = new BranchRegisterResponse();
    exceptionHandleTemplate(new AbstractCallback<BranchRegisterRequest, BranchRegisterResponse>() {

        @Override
        public void execute(BranchRegisterRequest request, BranchRegisterResponse response) throws TransactionException {
            try {
                doBranchRegister(request, response, rpcContext);
            } catch (StoreException e) {
                throw new TransactionException(TransactionExceptionCode.FailedStore, String.format("branch register request failed. xid=%s, msg=%s", request.getXid(), e.getMessage()), e);
            }
        }
    }, request, response);
    return response;
}
Also used : TransactionException(io.seata.core.exception.TransactionException) BranchRegisterResponse(io.seata.core.protocol.transaction.BranchRegisterResponse) BranchRegisterRequest(io.seata.core.protocol.transaction.BranchRegisterRequest) StoreException(io.seata.common.exception.StoreException)

Aggregations

StoreException (io.seata.common.exception.StoreException)39 PreparedStatement (java.sql.PreparedStatement)20 SQLException (java.sql.SQLException)20 Connection (java.sql.Connection)18 TransactionException (io.seata.core.exception.TransactionException)7 RedisException (io.seata.common.exception.RedisException)4 ResultSet (java.sql.ResultSet)4 Jedis (redis.clients.jedis.Jedis)4 StringJoiner (java.util.StringJoiner)3 LockDO (io.seata.core.store.LockDO)2 StoreMode (io.seata.core.store.StoreMode)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 Transaction (redis.clients.jedis.Transaction)2 BranchRegisterRequest (io.seata.core.protocol.transaction.BranchRegisterRequest)1 BranchRegisterResponse (io.seata.core.protocol.transaction.BranchRegisterResponse)1 BranchReportRequest (io.seata.core.protocol.transaction.BranchReportRequest)1 BranchReportResponse (io.seata.core.protocol.transaction.BranchReportResponse)1 GlobalBeginRequest (io.seata.core.protocol.transaction.GlobalBeginRequest)1 GlobalBeginResponse (io.seata.core.protocol.transaction.GlobalBeginResponse)1