Search in sources :

Example 1 with LockDO

use of io.seata.core.store.LockDO in project seata by seata.

the class LockStoreDataBaseDAO method doAcquireLocks.

/**
 * Do acquire lock boolean.
 *
 * @param conn    the conn
 * @param lockDOs the lock do list
 * @return the boolean
 */
protected boolean doAcquireLocks(Connection conn, List<LockDO> lockDOs) {
    PreparedStatement ps = null;
    try {
        // insert
        String insertLockSQL = LockStoreSqlFactory.getLogStoreSql(dbType).getInsertLockSQL(lockTable);
        ps = conn.prepareStatement(insertLockSQL);
        for (LockDO lockDO : lockDOs) {
            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());
            ps.addBatch();
        }
        return ps.executeBatch().length == lockDOs.size();
    } catch (SQLException e) {
        LOGGER.error("Global lock batch acquire error: {}", e.getMessage(), e);
        // return false,let the caller go to conn.rollabck()
        return false;
    } finally {
        IOUtil.close(ps);
    }
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) LockDO(io.seata.core.store.LockDO)

Example 2 with LockDO

use of io.seata.core.store.LockDO in project seata by seata.

the class AbstractLocker method convertToLockDO.

/**
 * Convert to lock do list.
 *
 * @param locks the locks
 * @return the list
 */
protected List<LockDO> convertToLockDO(List<RowLock> locks) {
    List<LockDO> lockDOs = new ArrayList<>();
    if (CollectionUtils.isEmpty(locks)) {
        return lockDOs;
    }
    for (RowLock rowLock : locks) {
        LockDO lockDO = new LockDO();
        lockDO.setBranchId(rowLock.getBranchId());
        lockDO.setPk(rowLock.getPk());
        lockDO.setResourceId(rowLock.getResourceId());
        lockDO.setRowKey(getRowKey(rowLock.getResourceId(), rowLock.getTableName(), rowLock.getPk()));
        lockDO.setXid(rowLock.getXid());
        lockDO.setTransactionId(rowLock.getTransactionId());
        lockDO.setTableName(rowLock.getTableName());
        lockDOs.add(lockDO);
    }
    return lockDOs;
}
Also used : ArrayList(java.util.ArrayList) LockDO(io.seata.core.store.LockDO)

Example 3 with LockDO

use of io.seata.core.store.LockDO in project seata by seata.

the class RedisLocker method releaseLock.

@Override
public boolean releaseLock(List<RowLock> rowLocks) {
    if (CollectionUtils.isEmpty(rowLocks)) {
        return true;
    }
    String currentXid = rowLocks.get(0).getXid();
    Long branchId = rowLocks.get(0).getBranchId();
    List<LockDO> needReleaseLocks = convertToLockDO(rowLocks);
    String[] needReleaseKeys = new String[needReleaseLocks.size()];
    for (int i = 0; i < needReleaseLocks.size(); i++) {
        needReleaseKeys[i] = buildLockKey(needReleaseLocks.get(i).getRowKey());
    }
    try (Jedis jedis = JedisPooledFactory.getJedisInstance()) {
        Pipeline pipelined = jedis.pipelined();
        pipelined.del(needReleaseKeys);
        pipelined.hdel(buildXidLockKey(currentXid), branchId.toString());
        pipelined.sync();
        return true;
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) LockDO(io.seata.core.store.LockDO) Pipeline(redis.clients.jedis.Pipeline)

Example 4 with LockDO

use of io.seata.core.store.LockDO in project seata by seata.

the class DataBaseLockStoreDAOTest method test_isLockable_cannot.

@Test
public void test_isLockable_cannot() throws SQLException {
    List<LockDO> lockDOs = new ArrayList<>();
    for (int i = 0; i < 3; i++) {
        LockDO lock = new LockDO();
        lock.setResourceId("abc");
        lock.setXid("abc-123:222");
        lock.setTransactionId(222L);
        lock.setBranchId((long) i);
        lock.setRowKey("abc-" + i);
        lock.setPk(String.valueOf(i));
        lock.setTableName("t");
        lockDOs.add(lock);
    }
    boolean ret = dataBaseLockStoreDAO.acquireLock(lockDOs);
    Assertions.assertTrue(ret);
    String sql = "select * from lock_table where xid = 'abc-123:222' and table_name  = 't' and row_key in ('abc-0','abc-1','abc-2')";
    Connection conn = null;
    try {
        conn = dataSource.getConnection();
        ResultSet rs = conn.createStatement().executeQuery(sql);
        if (rs.next()) {
            Assertions.assertTrue(true);
        } else {
            Assertions.assertTrue(false);
        }
    } finally {
        IOUtil.close(conn);
    }
    List<LockDO> lockDOs_2 = new ArrayList<>();
    for (int i = 0; i < 3; i++) {
        LockDO lock = new LockDO();
        lock.setResourceId("abc");
        lock.setXid("abc-123:333");
        lock.setTransactionId(333L);
        lock.setBranchId((long) i);
        lock.setRowKey("abc-" + i);
        lock.setPk(String.valueOf(i));
        lock.setTableName("t");
        lockDOs_2.add(lock);
    }
    boolean ret2 = dataBaseLockStoreDAO.acquireLock(lockDOs_2);
    Assertions.assertTrue(!ret2);
}
Also used : ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) LockDO(io.seata.core.store.LockDO) Test(org.junit.jupiter.api.Test)

Example 5 with LockDO

use of io.seata.core.store.LockDO in project seata by seata.

the class DataBaseLockStoreDAOTest method test_re_acquireLocks.

@Test
public void test_re_acquireLocks() throws SQLException {
    List<LockDO> lockDOs = new ArrayList<>();
    for (int i = 0; i < 3; i++) {
        LockDO lock = new LockDO();
        lock.setResourceId("abc");
        lock.setXid("abc-123:123");
        lock.setTransactionId(123L);
        lock.setBranchId((long) i);
        lock.setRowKey("abc-" + i);
        lock.setPk(String.valueOf(i));
        lock.setTableName("t");
        lockDOs.add(lock);
    }
    boolean ret = dataBaseLockStoreDAO.acquireLock(lockDOs);
    Assertions.assertTrue(ret);
    String sql = "select * from lock_table where xid = 'abc-123:123' and table_name  = 't' and row_key in ('abc-0','abc-1','abc-2')";
    Connection conn = null;
    try {
        conn = dataSource.getConnection();
        ResultSet rs = conn.createStatement().executeQuery(sql);
        if (rs.next()) {
            Assertions.assertTrue(true);
        } else {
            Assertions.assertTrue(false);
        }
    } finally {
        IOUtil.close(conn);
    }
    // lock again
    Assertions.assertTrue(dataBaseLockStoreDAO.acquireLock(lockDOs));
    Assertions.assertTrue(dataBaseLockStoreDAO.unLock(lockDOs));
}
Also used : ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) LockDO(io.seata.core.store.LockDO) Test(org.junit.jupiter.api.Test)

Aggregations

LockDO (io.seata.core.store.LockDO)16 ArrayList (java.util.ArrayList)8 Connection (java.sql.Connection)6 ResultSet (java.sql.ResultSet)6 Jedis (redis.clients.jedis.Jedis)6 Pipeline (redis.clients.jedis.Pipeline)6 Test (org.junit.jupiter.api.Test)5 PreparedStatement (java.sql.PreparedStatement)4 SQLException (java.sql.SQLException)4 StoreException (io.seata.common.exception.StoreException)2 HashSet (java.util.HashSet)2 List (java.util.List)2 StringJoiner (java.util.StringJoiner)2 HashMap (java.util.HashMap)1