Search in sources :

Example 1 with RedisException

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

the class RedisTransactionStoreManager method updateBranchTransactionDO.

/**
 * Update the branch transaction
 * @param branchTransactionDO
 * @return
 */
private boolean updateBranchTransactionDO(BranchTransactionDO branchTransactionDO) {
    String branchKey = buildBranchKey(branchTransactionDO.getBranchId());
    try (Jedis jedis = JedisPooledFactory.getJedisInstance()) {
        String previousBranchStatus = jedis.hget(branchKey, REDIS_KEY_BRANCH_STATUS);
        if (StringUtils.isEmpty(previousBranchStatus)) {
            throw new StoreException("Branch transaction is not exist, update branch transaction failed.");
        }
        Map<String, String> map = new HashMap<>(2, 1);
        map.put(REDIS_KEY_BRANCH_STATUS, String.valueOf(branchTransactionDO.getStatus()));
        map.put(REDIS_KEY_BRANCH_GMT_MODIFIED, String.valueOf((new Date()).getTime()));
        jedis.hmset(branchKey, map);
        return true;
    } catch (Exception ex) {
        throw new RedisException(ex);
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) HashMap(java.util.HashMap) RedisException(io.seata.common.exception.RedisException) Date(java.util.Date) StoreException(io.seata.common.exception.StoreException) RedisException(io.seata.common.exception.RedisException) StoreException(io.seata.common.exception.StoreException)

Example 2 with RedisException

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

the class RedisTransactionStoreManager method deleteBranchTransactionDO.

/**
 * Delete the branch transaction
 * @param branchTransactionDO
 * @return
 */
private boolean deleteBranchTransactionDO(BranchTransactionDO branchTransactionDO) {
    String branchKey = buildBranchKey(branchTransactionDO.getBranchId());
    try (Jedis jedis = JedisPooledFactory.getJedisInstance()) {
        Map<String, String> branchTransactionDOMap = jedis.hgetAll(branchKey);
        String xid = branchTransactionDOMap.get(REDIS_KEY_BRANCH_XID);
        if (StringUtils.isEmpty(xid)) {
            return true;
        }
        String branchListKey = buildBranchListKeyByXid(branchTransactionDO.getXid());
        Pipeline pipelined = jedis.pipelined();
        pipelined.lrem(branchListKey, 0, branchKey);
        pipelined.del(branchKey);
        pipelined.sync();
        return true;
    } catch (Exception ex) {
        throw new RedisException(ex);
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) RedisException(io.seata.common.exception.RedisException) StoreException(io.seata.common.exception.StoreException) RedisException(io.seata.common.exception.RedisException) Pipeline(redis.clients.jedis.Pipeline)

Example 3 with RedisException

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

the class RedisTransactionStoreManager method insertGlobalTransactionDO.

/**
 * Insert the global transaction.
 * @param globalTransactionDO
 * @return
 */
private boolean insertGlobalTransactionDO(GlobalTransactionDO globalTransactionDO) {
    String globalKey = buildGlobalKeyByTransactionId(globalTransactionDO.getTransactionId());
    try (Jedis jedis = JedisPooledFactory.getJedisInstance()) {
        Date now = new Date();
        globalTransactionDO.setGmtCreate(now);
        globalTransactionDO.setGmtModified(now);
        Pipeline pipelined = jedis.pipelined();
        pipelined.hmset(globalKey, BeanUtils.objectToMap(globalTransactionDO));
        pipelined.rpush(buildGlobalStatus(globalTransactionDO.getStatus()), globalTransactionDO.getXid());
        pipelined.sync();
        return true;
    } catch (Exception ex) {
        throw new RedisException(ex);
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) RedisException(io.seata.common.exception.RedisException) Date(java.util.Date) StoreException(io.seata.common.exception.StoreException) RedisException(io.seata.common.exception.RedisException) Pipeline(redis.clients.jedis.Pipeline)

Example 4 with RedisException

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

the class RedisTransactionStoreManager method deleteGlobalTransactionDO.

/**
 * Delete the global transaction.
 * It will operate two parts:
 *  1.delete the global session map
 *  2.remove the xid from the global status list
 * If the operate failed,the succeed operates will rollback
 * @param globalTransactionDO
 * @return
 */
private boolean deleteGlobalTransactionDO(GlobalTransactionDO globalTransactionDO) {
    String globalKey = buildGlobalKeyByTransactionId(globalTransactionDO.getTransactionId());
    try (Jedis jedis = JedisPooledFactory.getJedisInstance()) {
        Map<String, String> globalTransactionDoMap = jedis.hgetAll(globalKey);
        String xid = globalTransactionDoMap.get(REDIS_KEY_GLOBAL_XID);
        if (StringUtils.isEmpty(xid)) {
            LOGGER.warn("Global transaction is not exist,xid = {}.Maybe has been deleted by another tc server", globalTransactionDO.getXid());
            return true;
        }
        Pipeline pipelined = jedis.pipelined();
        pipelined.lrem(buildGlobalStatus(globalTransactionDO.getStatus()), 0, globalTransactionDO.getXid());
        pipelined.del(globalKey);
        pipelined.sync();
        return true;
    } catch (Exception ex) {
        throw new RedisException(ex);
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) RedisException(io.seata.common.exception.RedisException) StoreException(io.seata.common.exception.StoreException) RedisException(io.seata.common.exception.RedisException) Pipeline(redis.clients.jedis.Pipeline)

Example 5 with RedisException

use of io.seata.common.exception.RedisException 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)

Aggregations

RedisException (io.seata.common.exception.RedisException)13 StoreException (io.seata.common.exception.StoreException)12 Jedis (redis.clients.jedis.Jedis)12 Pipeline (redis.clients.jedis.Pipeline)8 Date (java.util.Date)4 HashMap (java.util.HashMap)2 Transaction (redis.clients.jedis.Transaction)2 HashSet (java.util.HashSet)1 JedisPool (redis.clients.jedis.JedisPool)1 JedisPoolConfig (redis.clients.jedis.JedisPoolConfig)1 JedisSentinelPool (redis.clients.jedis.JedisSentinelPool)1