Search in sources :

Example 11 with StoreException

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

the class LogStoreDataBaseDAO method updateBranchTransactionDO.

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

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

the class LogStoreDataBaseDAO method insertBranchTransactionDO.

@Override
public boolean insertBranchTransactionDO(BranchTransactionDO branchTransactionDO) {
    String sql = LogStoreSqlsFactory.getLogStoreSqls(dbType).getInsertBranchTransactionSQL(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.getTransactionId());
        ps.setLong(3, branchTransactionDO.getBranchId());
        ps.setString(4, branchTransactionDO.getResourceGroupId());
        ps.setString(5, branchTransactionDO.getResourceId());
        ps.setString(6, branchTransactionDO.getBranchType());
        ps.setInt(7, branchTransactionDO.getStatus());
        ps.setString(8, branchTransactionDO.getClientId());
        ps.setString(9, branchTransactionDO.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 13 with StoreException

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

the class LockStoreDataBaseDAO method unLock.

@Override
public boolean unLock(String xid, List<Long> branchIds) {
    Connection conn = null;
    PreparedStatement ps = null;
    try {
        conn = lockStoreDataSource.getConnection();
        conn.setAutoCommit(true);
        StringJoiner sj = new StringJoiner(",");
        branchIds.forEach(branchId -> sj.add("?"));
        // batch release lock by branch list
        String batchDeleteSQL = LockStoreSqlFactory.getLogStoreSql(dbType).getBatchDeleteLockSqlByBranchs(lockTable, sj.toString());
        ps = conn.prepareStatement(batchDeleteSQL);
        ps.setString(1, xid);
        for (int i = 0; i < branchIds.size(); i++) {
            ps.setLong(i + 2, branchIds.get(i));
        }
        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 14 with StoreException

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

the class LockStoreDataBaseDAO method acquireLock.

@Override
public boolean acquireLock(List<LockDO> lockDOs) {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    Set<String> dbExistedRowKeys = new HashSet<>();
    boolean originalAutoCommit = true;
    if (lockDOs.size() > 1) {
        lockDOs = lockDOs.stream().filter(LambdaUtils.distinctByKey(LockDO::getRowKey)).collect(Collectors.toList());
    }
    try {
        conn = lockStoreDataSource.getConnection();
        if (originalAutoCommit = conn.getAutoCommit()) {
            conn.setAutoCommit(false);
        }
        // check lock
        StringJoiner sj = new StringJoiner(",");
        for (int i = 0; i < lockDOs.size(); i++) {
            sj.add("?");
        }
        boolean canLock = true;
        // query
        String checkLockSQL = LockStoreSqlFactory.getLogStoreSql(dbType).getCheckLockableSql(lockTable, sj.toString());
        ps = conn.prepareStatement(checkLockSQL);
        for (int i = 0; i < lockDOs.size(); i++) {
            ps.setString(i + 1, lockDOs.get(i).getRowKey());
        }
        rs = ps.executeQuery();
        String currentXID = lockDOs.get(0).getXid();
        while (rs.next()) {
            String dbXID = rs.getString(ServerTableColumnsName.LOCK_TABLE_XID);
            if (!StringUtils.equals(dbXID, currentXID)) {
                if (LOGGER.isInfoEnabled()) {
                    String dbPk = rs.getString(ServerTableColumnsName.LOCK_TABLE_PK);
                    String dbTableName = rs.getString(ServerTableColumnsName.LOCK_TABLE_TABLE_NAME);
                    Long dbBranchId = rs.getLong(ServerTableColumnsName.LOCK_TABLE_BRANCH_ID);
                    LOGGER.info("Global lock on [{}:{}] is holding by xid {} branchId {}", dbTableName, dbPk, dbXID, dbBranchId);
                }
                canLock &= false;
                break;
            }
            dbExistedRowKeys.add(rs.getString(ServerTableColumnsName.LOCK_TABLE_ROW_KEY));
        }
        if (!canLock) {
            conn.rollback();
            return false;
        }
        List<LockDO> unrepeatedLockDOs = null;
        if (CollectionUtils.isNotEmpty(dbExistedRowKeys)) {
            unrepeatedLockDOs = lockDOs.stream().filter(lockDO -> !dbExistedRowKeys.contains(lockDO.getRowKey())).collect(Collectors.toList());
        } else {
            unrepeatedLockDOs = lockDOs;
        }
        if (CollectionUtils.isEmpty(unrepeatedLockDOs)) {
            conn.rollback();
            return true;
        }
        // lock
        if (unrepeatedLockDOs.size() == 1) {
            LockDO lockDO = unrepeatedLockDOs.get(0);
            if (!doAcquireLock(conn, lockDO)) {
                if (LOGGER.isInfoEnabled()) {
                    LOGGER.info("Global lock acquire failed, xid {} branchId {} pk {}", lockDO.getXid(), lockDO.getBranchId(), lockDO.getPk());
                }
                conn.rollback();
                return false;
            }
        } else {
            if (!doAcquireLocks(conn, unrepeatedLockDOs)) {
                if (LOGGER.isInfoEnabled()) {
                    LOGGER.info("Global lock batch acquire failed, xid {} branchId {} pks {}", unrepeatedLockDOs.get(0).getXid(), unrepeatedLockDOs.get(0).getBranchId(), unrepeatedLockDOs.stream().map(lockDO -> lockDO.getPk()).collect(Collectors.toList()));
                }
                conn.rollback();
                return false;
            }
        }
        conn.commit();
        return true;
    } catch (SQLException e) {
        throw new StoreException(e);
    } finally {
        IOUtil.close(rs, ps);
        if (conn != null) {
            try {
                if (originalAutoCommit) {
                    conn.setAutoCommit(true);
                }
                conn.close();
            } catch (SQLException e) {
            }
        }
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) LockDO(io.seata.core.store.LockDO) StoreException(io.seata.common.exception.StoreException) ResultSet(java.sql.ResultSet)

Example 15 with StoreException

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

the class LockStoreDataBaseDAO method unLock.

@Override
public boolean unLock(String xid, Long branchId) {
    Connection conn = null;
    PreparedStatement ps = null;
    try {
        conn = lockStoreDataSource.getConnection();
        conn.setAutoCommit(true);
        // batch release lock by branch
        String batchDeleteSQL = LockStoreSqlFactory.getLogStoreSql(dbType).getBatchDeleteLockSqlByBranch(lockTable);
        ps = conn.prepareStatement(batchDeleteSQL);
        ps.setString(1, xid);
        ps.setLong(2, branchId);
        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)

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