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);
}
}
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;
}
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;
}
}
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);
}
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));
}
Aggregations