Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: SQLIntegrityConstraintViolationException capture incorrectly when inserting a globallock #6833

Merged
merged 9 commits into from
Sep 11, 2024
2 changes: 2 additions & 0 deletions changes/en-us/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ Add changes here for all PR submitted to the 2.x branch.
- [[#6817](https://github.com/apache/incubator-seata/pull/6817)] bugfix: fix namingserver changVgroup failed
- [[#6820](https://github.com/apache/incubator-seata/pull/6820)] Fix file path error in the Dockerfile
- [[#6825](https://github.com/apache/incubator-seata/pull/6825)] Fix the issue of XA mode transaction timeout and inability to roll back in Postgres
- [[#6833](https://github.com/apache/incubator-seata/pull/6833)] SQLIntegrityConstraintViolationException capture incorrectly when inserting a globallock
- [[#6835](https://github.com/apache/incubator-seata/pull/6835)] Fix the issue of missing request body of post method in HttpClientUtil



### optimize:
- [[#6499](https://github.com/apache/incubator-seata/pull/6499)] split the task thread pool for committing and rollbacking statuses
- [[#6208](https://github.com/apache/incubator-seata/pull/6208)] optimize : load SeataSerializer by version
Expand Down
2 changes: 2 additions & 0 deletions changes/zh-cn/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@
- [[#6817](https://github.com/apache/incubator-seata/pull/6817)] 修复namingserver切换事务分组失效的问题
- [[#6820](https://github.com/apache/incubator-seata/pull/6820)] 修复Dockerfile得文件结构错误
- [[#6825](https://github.com/apache/incubator-seata/pull/6825)] 修复Postgres的XA模式事务超时无法回滚问题
- [[#6833](https://github.com/apache/incubator-seata/pull/6833)] 插入全局锁时 SQLIntegrityConstraintViolationException 捕获不正确
- [[#6835](https://github.com/apache/incubator-seata/pull/6835)] 修复HttpClientUtil中post方法请求体缺失的问题


### optimize:
- [[#6499](https://github.com/apache/incubator-seata/pull/6499)] 拆分 committing 和 rollbacking 状态的任务线程池
- [[#6208](https://github.com/apache/incubator-seata/pull/6208)] 支持多版本的Seata序列化
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.seata.server.storage.db.lock;

import java.sql.BatchUpdateException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
Expand Down Expand Up @@ -372,10 +373,23 @@ protected boolean doAcquireLocks(Connection conn, List<LockDO> lockDOs) throws S
return ps.executeBatch().length == lockDOs.size();
} catch (SQLIntegrityConstraintViolationException e) {
LOGGER.error("Global lock batch acquire error: {}", e.getMessage(), e);
//return false,let the caller go to conn.rollabck()
//return false,let the caller go to conn.rollback()
return false;
} catch (SQLException e) {
throw e;
} catch (BatchUpdateException e) {
Throwable cause = e.getCause();
if (cause != null) {
if (cause instanceof SQLIntegrityConstraintViolationException) {
return false;
}
throw e;
}
SQLException nextException = e.getNextException();
if (nextException == null) {
throw e;
} else if (nextException instanceof SQLIntegrityConstraintViolationException) {
return false;
}
throw nextException;
changeAtLater marked this conversation as resolved.
Show resolved Hide resolved
} finally {
IOUtil.close(ps);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import java.util.ArrayList;
import java.util.List;

import org.apache.seata.common.ConfigurationKeys;
import org.apache.seata.common.util.IOUtil;
import org.apache.seata.config.ConfigurationFactory;
import org.apache.seata.core.store.LockDO;
import org.apache.seata.server.storage.db.lock.LockStoreDataBaseDAO;
import org.apache.commons.dbcp2.BasicDataSource;
Expand Down Expand Up @@ -52,9 +54,9 @@ public static void start(ApplicationContext context){
dataSource.setUsername("sa");
dataSource.setPassword("");

ConfigurationFactory.getInstance().putConfig(ConfigurationKeys.STORE_DB_TYPE, "h2");
ConfigurationFactory.getInstance().putConfig(ConfigurationKeys.LOCK_DB_TABLE, "lock_table");
dataBaseLockStoreDAO = new LockStoreDataBaseDAO(dataSource);
dataBaseLockStoreDAO.setDbType("h2");
dataBaseLockStoreDAO.setLockTable("lock_table");

prepareTable(dataSource);
}
Expand Down Expand Up @@ -88,7 +90,7 @@ public void test_acquireLocks() throws SQLException {
lock.setXid("abc-123:123");
lock.setTransactionId(123L);
lock.setBranchId((long) i);
lock.setRowKey("abc-"+i);
lock.setRowKey("test_acquireLocks-"+i);
lock.setPk(String.valueOf(i));
lock.setTableName("t");
lockDOs.add(lock);
Expand All @@ -97,7 +99,8 @@ public void test_acquireLocks() throws SQLException {
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')" ;
String sql = "select * from lock_table where xid = 'abc-123:123' and table_name = 't' " +
"and row_key in ('test_acquireLocks-0','test_acquireLocks-1','test_acquireLocks-2')" ;
Connection conn = null;
ResultSet rs = null;
try{
Expand Down Expand Up @@ -126,7 +129,7 @@ public void test_re_acquireLocks() throws SQLException {
lock.setXid("abc-123:123");
lock.setTransactionId(123L);
lock.setBranchId((long) i);
lock.setRowKey("abc-"+i);
lock.setRowKey("test_re_acquireLocks-"+i);
lock.setPk(String.valueOf(i));
lock.setTableName("t");
lockDOs.add(lock);
Expand All @@ -135,7 +138,8 @@ public void test_re_acquireLocks() throws SQLException {
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')" ;
String sql = "select * from lock_table where xid = 'abc-123:123' and table_name = 't' " +
"and row_key in ('test_re_acquireLocks-0','test_re_acquireLocks-1','test_re_acquireLocks-2')" ;
Connection conn = null;
ResultSet rs = null;
try{
Expand Down Expand Up @@ -166,7 +170,7 @@ public void tes_unLocks() throws SQLException {
lock.setXid("abc-456:123");
lock.setTransactionId(123L);
lock.setBranchId((long) i);
lock.setRowKey("abc-"+i);
lock.setRowKey("tes_unLocks-"+i);
lock.setPk(String.valueOf(i));
lock.setTableName("t");
lockDOs.add(lock);
Expand All @@ -175,7 +179,8 @@ public void tes_unLocks() throws SQLException {
boolean ret = dataBaseLockStoreDAO.acquireLock(lockDOs);
Assertions.assertTrue(ret);

String sql = "select * from lock_table where xid = 'abc-456:123' and table_name = 't' and row_key in ('abc-0','abc-1','abc-2')" ;
String sql = "select * from lock_table where xid = 'abc-456:123' and table_name = 't' " +
"and row_key in ('tes_unLocks-0','tes_unLocks-1','tes_unLocks-2')" ;
Connection conn = null;
ResultSet rs = null;
try{
Expand Down Expand Up @@ -214,7 +219,7 @@ public void test_isLockable_can(){
lock.setXid("abc-678:123");
lock.setTransactionId(123L);
lock.setBranchId((long) i);
lock.setRowKey("abc-"+i);
lock.setRowKey("test_isLockable_can-"+i);
lock.setPk(String.valueOf(i));
lock.setTableName("t");
lockDOs.add(lock);
Expand All @@ -236,7 +241,7 @@ public void test_isLockable_cannot() throws SQLException {
lock.setXid("abc-123:222");
lock.setTransactionId(222L);
lock.setBranchId((long) i);
lock.setRowKey("abc-"+i);
lock.setRowKey("test_isLockable_cannot-"+i);
lock.setPk(String.valueOf(i));
lock.setTableName("t");
lockDOs.add(lock);
Expand All @@ -245,7 +250,8 @@ public void test_isLockable_cannot() throws SQLException {
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')" ;
String sql = "select * from lock_table where xid = 'abc-123:222' and table_name = 't' " +
"and row_key in ('test_isLockable_cannot-0','test_isLockable_cannot-1','test_isLockable_cannot-2')" ;
Connection conn = null;
ResultSet rs = null;
try{
Expand All @@ -267,7 +273,7 @@ public void test_isLockable_cannot() throws SQLException {
lock.setXid("abc-123:333");
lock.setTransactionId(333L);
lock.setBranchId((long) i);
lock.setRowKey("abc-"+i);
lock.setRowKey("test_isLockable_cannot-"+i);
lock.setPk(String.valueOf(i));
lock.setTableName("t");
lockDOs_2.add(lock);
Expand All @@ -278,6 +284,58 @@ public void test_isLockable_cannot() throws SQLException {

}

@Test
public void test_isLockable_cannot1() 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(1L);
lock.setRowKey("test_isLockable_cannot1-"+i);
lock.setPk(String.valueOf(i));
lock.setTableName("t");
lockDOs.add(lock);
}

boolean ret = dataBaseLockStoreDAO.acquireLock(lockDOs, true, true);
Assertions.assertTrue(ret);

String sql = "select * from lock_table where xid = 'abc-123:222' and table_name = 't' " +
"and row_key in ('test_isLockable_cannot1-0','test_isLockable_cannot1-1','test_isLockable_cannot1-2')" ;
Connection conn = null;
ResultSet rs = null;
try{
conn = dataSource.getConnection();
rs = conn.createStatement().executeQuery(sql);
if (rs.next()) {
Assertions.assertTrue(true);
} else {
Assertions.fail();
}
} finally {
IOUtil.close(rs, 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(2L);
lock.setRowKey("test_isLockable_cannot1-"+i);
lock.setPk(String.valueOf(i));
lock.setTableName("t");
lockDOs_2.add(lock);
}

boolean ret2 = dataBaseLockStoreDAO.acquireLock(lockDOs_2, true, true);
Assertions.assertFalse(ret2);

}

@AfterAll
public static void clearStoreDB(){
FileUtils.deleteRecursive("db_store", true);
Expand Down
Loading