Search in sources :

Example 26 with StoreException

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

the class AbstractTCInboundHandler method handle.

@Override
public GlobalCommitResponse handle(GlobalCommitRequest request, final RpcContext rpcContext) {
    GlobalCommitResponse response = new GlobalCommitResponse();
    response.setGlobalStatus(GlobalStatus.Committing);
    exceptionHandleTemplate(new AbstractCallback<GlobalCommitRequest, GlobalCommitResponse>() {

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

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

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

Example 27 with StoreException

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

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 : HashMap(java.util.HashMap) Date(java.util.Date) 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 28 with StoreException

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

the class LogStoreDataBaseDAO method insertGlobalTransactionDO.

@Override
public boolean insertGlobalTransactionDO(GlobalTransactionDO globalTransactionDO) {
    String sql = LogStoreSqlsFactory.getLogStoreSqls(dbType).getInsertGlobalTransactionSQL(globalTable);
    Connection conn = null;
    PreparedStatement ps = null;
    try {
        conn = logStoreDataSource.getConnection();
        conn.setAutoCommit(true);
        ps = conn.prepareStatement(sql);
        ps.setString(1, globalTransactionDO.getXid());
        ps.setLong(2, globalTransactionDO.getTransactionId());
        ps.setInt(3, globalTransactionDO.getStatus());
        ps.setString(4, globalTransactionDO.getApplicationId());
        ps.setString(5, globalTransactionDO.getTransactionServiceGroup());
        String transactionName = globalTransactionDO.getTransactionName();
        transactionName = transactionName.length() > transactionNameColumnSize ? transactionName.substring(0, transactionNameColumnSize) : transactionName;
        ps.setString(6, transactionName);
        ps.setInt(7, globalTransactionDO.getTimeout());
        ps.setLong(8, globalTransactionDO.getBeginTime());
        ps.setString(9, globalTransactionDO.getApplicationData());
        return ps.executeUpdate() > 0;
    } catch (SQLException e) {
        throw new StoreException(e);
    } finally {
        IOUtil.close(ps, conn);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) StoreException(io.seata.common.exception.StoreException)

Example 29 with StoreException

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

the class LogStoreDataBaseDAO method deleteGlobalTransactionDO.

@Override
public boolean deleteGlobalTransactionDO(GlobalTransactionDO globalTransactionDO) {
    String sql = LogStoreSqlsFactory.getLogStoreSqls(dbType).getDeleteGlobalTransactionSQL(globalTable);
    Connection conn = null;
    PreparedStatement ps = null;
    try {
        conn = logStoreDataSource.getConnection();
        conn.setAutoCommit(true);
        ps = conn.prepareStatement(sql);
        ps.setString(1, globalTransactionDO.getXid());
        ps.executeUpdate();
    } catch (SQLException e) {
        throw new StoreException(e);
    } finally {
        IOUtil.close(ps, conn);
    }
    return true;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) StoreException(io.seata.common.exception.StoreException)

Example 30 with StoreException

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

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)

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