Skip to content

bugfix: SQLIntegrityConstraintViolationException capture incorrectly when inserting a globallock #6833

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

Merged
merged 9 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
} 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 @@ -278,6 +280,57 @@ 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("abc-"+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 ('abc-0','abc-1','abc-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("abc-"+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