Search in sources :

Example 6 with StoreException

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

the class LockStoreDataBaseDAO method unLock.

@Override
public boolean unLock(List<LockDO> lockDOs) {
    Connection conn = null;
    PreparedStatement ps = null;
    try {
        conn = lockStoreDataSource.getConnection();
        conn.setAutoCommit(true);
        StringJoiner sj = new StringJoiner(",");
        for (int i = 0; i < lockDOs.size(); i++) {
            sj.add("?");
        }
        // batch release lock
        String batchDeleteSQL = LockStoreSqlFactory.getLogStoreSql(dbType).getBatchDeleteLockSql(lockTable, sj.toString());
        ps = conn.prepareStatement(batchDeleteSQL);
        ps.setString(1, lockDOs.get(0).getXid());
        for (int i = 0; i < lockDOs.size(); i++) {
            ps.setString(i + 2, lockDOs.get(i).getRowKey());
        }
        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) StringJoiner(java.util.StringJoiner) StoreException(io.seata.common.exception.StoreException)

Example 7 with StoreException

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

the class LockStoreDataBaseDAO method doAcquireLock.

/**
 * Do acquire lock boolean.
 *
 * @param conn   the conn
 * @param lockDO the lock do
 * @return the boolean
 */
protected boolean doAcquireLock(Connection conn, LockDO lockDO) {
    PreparedStatement ps = null;
    try {
        // insert
        String insertLockSQL = LockStoreSqlFactory.getLogStoreSql(dbType).getInsertLockSQL(lockTable);
        ps = conn.prepareStatement(insertLockSQL);
        ps.setString(1, lockDO.getXid());
        ps.setLong(2, lockDO.getTransactionId());
        ps.setLong(3, lockDO.getBranchId());
        ps.setString(4, lockDO.getResourceId());
        ps.setString(5, lockDO.getTableName());
        ps.setString(6, lockDO.getPk());
        ps.setString(7, lockDO.getRowKey());
        return ps.executeUpdate() > 0;
    } catch (SQLException e) {
        throw new StoreException(e);
    } finally {
        IOUtil.close(ps);
    }
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) StoreException(io.seata.common.exception.StoreException)

Example 8 with StoreException

use of io.seata.common.exception.StoreException 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 9 with StoreException

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

the class LogStoreDataBaseDAO method deleteBranchTransactionDO.

@Override
public boolean deleteBranchTransactionDO(BranchTransactionDO branchTransactionDO) {
    String sql = LogStoreSqlsFactory.getLogStoreSqls(dbType).getDeleteBranchTransactionByBranchIdSQL(branchTable);
    Connection conn = null;
    PreparedStatement ps = null;
    try {
        conn = logStoreDataSource.getConnection();
        conn.setAutoCommit(true);
        ps = conn.prepareStatement(sql);
        ps.setString(1, branchTransactionDO.getXid());
        ps.setLong(2, branchTransactionDO.getBranchId());
        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 10 with StoreException

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

the class LogStoreDataBaseDAO method updateGlobalTransactionDO.

@Override
public boolean updateGlobalTransactionDO(GlobalTransactionDO globalTransactionDO) {
    String sql = LogStoreSqlsFactory.getLogStoreSqls(dbType).getUpdateGlobalTransactionStatusSQL(globalTable);
    Connection conn = null;
    PreparedStatement ps = null;
    try {
        conn = logStoreDataSource.getConnection();
        conn.setAutoCommit(true);
        ps = conn.prepareStatement(sql);
        ps.setInt(1, globalTransactionDO.getStatus());
        ps.setString(2, globalTransactionDO.getXid());
        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)

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