Search in sources :

Example 1 with DataAccessException

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

the class LockStoreDataBaseDAO method checkLockable.

/**
 * Check lock boolean.
 *
 * @param conn    the conn
 * @param lockDOs the lock do
 * @return the boolean
 */
protected boolean checkLockable(Connection conn, List<LockDO> lockDOs) {
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
        StringJoiner sj = new StringJoiner(",");
        for (int i = 0; i < lockDOs.size(); i++) {
            sj.add("?");
        }
        // 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();
        while (rs.next()) {
            String xid = rs.getString("xid");
            if (!StringUtils.equals(xid, lockDOs.get(0).getXid())) {
                return false;
            }
        }
        return true;
    } catch (SQLException e) {
        throw new DataAccessException(e);
    } finally {
        IOUtil.close(rs, ps);
    }
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) StringJoiner(java.util.StringJoiner) DataAccessException(io.seata.common.exception.DataAccessException)

Example 2 with DataAccessException

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

the class LogStoreDataBaseDAO method getCurrentMaxSessionId.

private long getCurrentMaxSessionId(String sql, long high, long low) {
    long max = 0;
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
        conn = logStoreDataSource.getConnection();
        conn.setAutoCommit(true);
        ps = conn.prepareStatement(sql);
        ps.setLong(1, high);
        ps.setLong(2, low);
        rs = ps.executeQuery();
        while (rs.next()) {
            max = rs.getLong(1);
        }
    } catch (SQLException e) {
        throw new DataAccessException(e);
    } finally {
        IOUtil.close(rs, ps, conn);
    }
    return max;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) DataAccessException(io.seata.common.exception.DataAccessException)

Example 3 with DataAccessException

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

the class LogStoreDataBaseDAO method queryBranchTransactionDO.

@Override
public List<BranchTransactionDO> queryBranchTransactionDO(String xid) {
    List<BranchTransactionDO> rets = new ArrayList<>();
    String sql = LogStoreSqlsFactory.getLogStoreSqls(dbType).getQueryBranchTransaction(branchTable);
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
        conn = logStoreDataSource.getConnection();
        conn.setAutoCommit(true);
        ps = conn.prepareStatement(sql);
        ps.setString(1, xid);
        rs = ps.executeQuery();
        while (rs.next()) {
            rets.add(convertBranchTransactionDO(rs));
        }
        return rets;
    } catch (SQLException e) {
        throw new DataAccessException(e);
    } finally {
        IOUtil.close(rs, ps, conn);
    }
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) BranchTransactionDO(io.seata.core.store.BranchTransactionDO) DataAccessException(io.seata.common.exception.DataAccessException)

Example 4 with DataAccessException

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

the class LogStoreDataBaseDAO method queryGlobalTransactionDO.

@Override
public GlobalTransactionDO queryGlobalTransactionDO(String xid) {
    String sql = LogStoreSqlsFactory.getLogStoreSqls(dbType).getQueryGlobalTransactionSQL(globalTable);
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
        conn = logStoreDataSource.getConnection();
        conn.setAutoCommit(true);
        ps = conn.prepareStatement(sql);
        ps.setString(1, xid);
        rs = ps.executeQuery();
        if (rs.next()) {
            return convertGlobalTransactionDO(rs);
        } else {
            return null;
        }
    } catch (SQLException e) {
        throw new DataAccessException(e);
    } finally {
        IOUtil.close(rs, ps, conn);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) DataAccessException(io.seata.common.exception.DataAccessException)

Example 5 with DataAccessException

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

the class LogStoreDataBaseDAO method queryGlobalTransactionDO.

@Override
public List<GlobalTransactionDO> queryGlobalTransactionDO(int[] statuses, int limit) {
    List<GlobalTransactionDO> ret = new ArrayList<>();
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
        conn = logStoreDataSource.getConnection();
        conn.setAutoCommit(true);
        String paramsPlaceHolder = org.apache.commons.lang.StringUtils.repeat("?", ",", statuses.length);
        String sql = LogStoreSqlsFactory.getLogStoreSqls(dbType).getQueryGlobalTransactionSQLByStatus(globalTable, paramsPlaceHolder);
        ps = conn.prepareStatement(sql);
        for (int i = 0; i < statuses.length; i++) {
            int status = statuses[i];
            ps.setInt(i + 1, status);
        }
        ps.setInt(statuses.length + 1, limit);
        rs = ps.executeQuery();
        while (rs.next()) {
            ret.add(convertGlobalTransactionDO(rs));
        }
        return ret;
    } catch (SQLException e) {
        throw new DataAccessException(e);
    } finally {
        IOUtil.close(rs, ps, conn);
    }
}
Also used : GlobalTransactionDO(io.seata.core.store.GlobalTransactionDO) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) DataAccessException(io.seata.common.exception.DataAccessException)

Aggregations

DataAccessException (io.seata.common.exception.DataAccessException)16 SQLException (java.sql.SQLException)10 Connection (java.sql.Connection)8 PreparedStatement (java.sql.PreparedStatement)8 ResultSet (java.sql.ResultSet)8 ArrayList (java.util.ArrayList)6 BranchTransactionDO (io.seata.core.store.BranchTransactionDO)4 GlobalTransactionDO (io.seata.core.store.GlobalTransactionDO)2 StringJoiner (java.util.StringJoiner)1