Skip to content

Commit b2f9c3a

Browse files
bugfix: SQLIntegrityConstraintViolationException capture incorrectly when inserting a globallock (apache#6833)
1 parent fde59c1 commit b2f9c3a

File tree

4 files changed

+91
-15
lines changed

4 files changed

+91
-15
lines changed

changes/en-us/2.x.md

+2
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@ Add changes here for all PR submitted to the 2.x branch.
3737
- [[#6817](https://github.com/apache/incubator-seata/pull/6817)] bugfix: fix namingserver changVgroup failed
3838
- [[#6820](https://github.com/apache/incubator-seata/pull/6820)] Fix file path error in the Dockerfile
3939
- [[#6825](https://github.com/apache/incubator-seata/pull/6825)] Fix the issue of XA mode transaction timeout and inability to roll back in Postgres
40+
- [[#6833](https://github.com/apache/incubator-seata/pull/6833)] SQLIntegrityConstraintViolationException capture incorrectly when inserting a globallock
4041
- [[#6835](https://github.com/apache/incubator-seata/pull/6835)] Fix the issue of missing request body of post method in HttpClientUtil
4142

4243

44+
4345
### optimize:
4446
- [[#6499](https://github.com/apache/incubator-seata/pull/6499)] split the task thread pool for committing and rollbacking statuses
4547
- [[#6208](https://github.com/apache/incubator-seata/pull/6208)] optimize : load SeataSerializer by version

changes/zh-cn/2.x.md

+2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@
3838
- [[#6817](https://github.com/apache/incubator-seata/pull/6817)] 修复namingserver切换事务分组失效的问题
3939
- [[#6820](https://github.com/apache/incubator-seata/pull/6820)] 修复Dockerfile得文件结构错误
4040
- [[#6825](https://github.com/apache/incubator-seata/pull/6825)] 修复Postgres的XA模式事务超时无法回滚问题
41+
- [[#6833](https://github.com/apache/incubator-seata/pull/6833)] 插入全局锁时 SQLIntegrityConstraintViolationException 捕获不正确
4142
- [[#6835](https://github.com/apache/incubator-seata/pull/6835)] 修复HttpClientUtil中post方法请求体缺失的问题
4243

44+
4345
### optimize:
4446
- [[#6499](https://github.com/apache/incubator-seata/pull/6499)] 拆分 committing 和 rollbacking 状态的任务线程池
4547
- [[#6208](https://github.com/apache/incubator-seata/pull/6208)] 支持多版本的Seata序列化

server/src/main/java/org/apache/seata/server/storage/db/lock/LockStoreDataBaseDAO.java

+17-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package org.apache.seata.server.storage.db.lock;
1818

19+
import java.sql.BatchUpdateException;
1920
import java.sql.Connection;
2021
import java.sql.PreparedStatement;
2122
import java.sql.ResultSet;
@@ -372,10 +373,23 @@ protected boolean doAcquireLocks(Connection conn, List<LockDO> lockDOs) throws S
372373
return ps.executeBatch().length == lockDOs.size();
373374
} catch (SQLIntegrityConstraintViolationException e) {
374375
LOGGER.error("Global lock batch acquire error: {}", e.getMessage(), e);
375-
//return false,let the caller go to conn.rollabck()
376+
//return false,let the caller go to conn.rollback()
376377
return false;
377-
} catch (SQLException e) {
378-
throw e;
378+
} catch (BatchUpdateException e) {
379+
Throwable cause = e.getCause();
380+
if (cause != null) {
381+
if (cause instanceof SQLIntegrityConstraintViolationException) {
382+
return false;
383+
}
384+
throw e;
385+
}
386+
SQLException nextException = e.getNextException();
387+
if (nextException == null) {
388+
throw e;
389+
} else if (nextException instanceof SQLIntegrityConstraintViolationException) {
390+
return false;
391+
}
392+
throw nextException;
379393
} finally {
380394
IOUtil.close(ps);
381395
}

server/src/test/java/org/apache/seata/server/lock/db/DataBaseLockStoreDAOTest.java

+70-12
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
import java.util.ArrayList;
2424
import java.util.List;
2525

26+
import org.apache.seata.common.ConfigurationKeys;
2627
import org.apache.seata.common.util.IOUtil;
28+
import org.apache.seata.config.ConfigurationFactory;
2729
import org.apache.seata.core.store.LockDO;
2830
import org.apache.seata.server.storage.db.lock.LockStoreDataBaseDAO;
2931
import org.apache.commons.dbcp2.BasicDataSource;
@@ -52,9 +54,9 @@ public static void start(ApplicationContext context){
5254
dataSource.setUsername("sa");
5355
dataSource.setPassword("");
5456

57+
ConfigurationFactory.getInstance().putConfig(ConfigurationKeys.STORE_DB_TYPE, "h2");
58+
ConfigurationFactory.getInstance().putConfig(ConfigurationKeys.LOCK_DB_TABLE, "lock_table");
5559
dataBaseLockStoreDAO = new LockStoreDataBaseDAO(dataSource);
56-
dataBaseLockStoreDAO.setDbType("h2");
57-
dataBaseLockStoreDAO.setLockTable("lock_table");
5860

5961
prepareTable(dataSource);
6062
}
@@ -88,7 +90,7 @@ public void test_acquireLocks() throws SQLException {
8890
lock.setXid("abc-123:123");
8991
lock.setTransactionId(123L);
9092
lock.setBranchId((long) i);
91-
lock.setRowKey("abc-"+i);
93+
lock.setRowKey("test_acquireLocks-"+i);
9294
lock.setPk(String.valueOf(i));
9395
lock.setTableName("t");
9496
lockDOs.add(lock);
@@ -97,7 +99,8 @@ public void test_acquireLocks() throws SQLException {
9799
boolean ret = dataBaseLockStoreDAO.acquireLock(lockDOs);
98100
Assertions.assertTrue(ret);
99101

100-
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')" ;
102+
String sql = "select * from lock_table where xid = 'abc-123:123' and table_name = 't' " +
103+
"and row_key in ('test_acquireLocks-0','test_acquireLocks-1','test_acquireLocks-2')" ;
101104
Connection conn = null;
102105
ResultSet rs = null;
103106
try{
@@ -126,7 +129,7 @@ public void test_re_acquireLocks() throws SQLException {
126129
lock.setXid("abc-123:123");
127130
lock.setTransactionId(123L);
128131
lock.setBranchId((long) i);
129-
lock.setRowKey("abc-"+i);
132+
lock.setRowKey("test_re_acquireLocks-"+i);
130133
lock.setPk(String.valueOf(i));
131134
lock.setTableName("t");
132135
lockDOs.add(lock);
@@ -135,7 +138,8 @@ public void test_re_acquireLocks() throws SQLException {
135138
boolean ret = dataBaseLockStoreDAO.acquireLock(lockDOs);
136139
Assertions.assertTrue(ret);
137140

138-
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')" ;
141+
String sql = "select * from lock_table where xid = 'abc-123:123' and table_name = 't' " +
142+
"and row_key in ('test_re_acquireLocks-0','test_re_acquireLocks-1','test_re_acquireLocks-2')" ;
139143
Connection conn = null;
140144
ResultSet rs = null;
141145
try{
@@ -166,7 +170,7 @@ public void tes_unLocks() throws SQLException {
166170
lock.setXid("abc-456:123");
167171
lock.setTransactionId(123L);
168172
lock.setBranchId((long) i);
169-
lock.setRowKey("abc-"+i);
173+
lock.setRowKey("tes_unLocks-"+i);
170174
lock.setPk(String.valueOf(i));
171175
lock.setTableName("t");
172176
lockDOs.add(lock);
@@ -175,7 +179,8 @@ public void tes_unLocks() throws SQLException {
175179
boolean ret = dataBaseLockStoreDAO.acquireLock(lockDOs);
176180
Assertions.assertTrue(ret);
177181

178-
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')" ;
182+
String sql = "select * from lock_table where xid = 'abc-456:123' and table_name = 't' " +
183+
"and row_key in ('tes_unLocks-0','tes_unLocks-1','tes_unLocks-2')" ;
179184
Connection conn = null;
180185
ResultSet rs = null;
181186
try{
@@ -214,7 +219,7 @@ public void test_isLockable_can(){
214219
lock.setXid("abc-678:123");
215220
lock.setTransactionId(123L);
216221
lock.setBranchId((long) i);
217-
lock.setRowKey("abc-"+i);
222+
lock.setRowKey("test_isLockable_can-"+i);
218223
lock.setPk(String.valueOf(i));
219224
lock.setTableName("t");
220225
lockDOs.add(lock);
@@ -236,7 +241,7 @@ public void test_isLockable_cannot() throws SQLException {
236241
lock.setXid("abc-123:222");
237242
lock.setTransactionId(222L);
238243
lock.setBranchId((long) i);
239-
lock.setRowKey("abc-"+i);
244+
lock.setRowKey("test_isLockable_cannot-"+i);
240245
lock.setPk(String.valueOf(i));
241246
lock.setTableName("t");
242247
lockDOs.add(lock);
@@ -245,7 +250,8 @@ public void test_isLockable_cannot() throws SQLException {
245250
boolean ret = dataBaseLockStoreDAO.acquireLock(lockDOs);
246251
Assertions.assertTrue(ret);
247252

248-
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')" ;
253+
String sql = "select * from lock_table where xid = 'abc-123:222' and table_name = 't' " +
254+
"and row_key in ('test_isLockable_cannot-0','test_isLockable_cannot-1','test_isLockable_cannot-2')" ;
249255
Connection conn = null;
250256
ResultSet rs = null;
251257
try{
@@ -267,7 +273,7 @@ public void test_isLockable_cannot() throws SQLException {
267273
lock.setXid("abc-123:333");
268274
lock.setTransactionId(333L);
269275
lock.setBranchId((long) i);
270-
lock.setRowKey("abc-"+i);
276+
lock.setRowKey("test_isLockable_cannot-"+i);
271277
lock.setPk(String.valueOf(i));
272278
lock.setTableName("t");
273279
lockDOs_2.add(lock);
@@ -278,6 +284,58 @@ public void test_isLockable_cannot() throws SQLException {
278284

279285
}
280286

287+
@Test
288+
public void test_isLockable_cannot1() throws SQLException {
289+
List<LockDO> lockDOs = new ArrayList<>();
290+
for(int i = 0; i < 3; i++) {
291+
LockDO lock = new LockDO();
292+
lock.setResourceId("abc");
293+
lock.setXid("abc-123:222");
294+
lock.setTransactionId(222L);
295+
lock.setBranchId(1L);
296+
lock.setRowKey("test_isLockable_cannot1-"+i);
297+
lock.setPk(String.valueOf(i));
298+
lock.setTableName("t");
299+
lockDOs.add(lock);
300+
}
301+
302+
boolean ret = dataBaseLockStoreDAO.acquireLock(lockDOs, true, true);
303+
Assertions.assertTrue(ret);
304+
305+
String sql = "select * from lock_table where xid = 'abc-123:222' and table_name = 't' " +
306+
"and row_key in ('test_isLockable_cannot1-0','test_isLockable_cannot1-1','test_isLockable_cannot1-2')" ;
307+
Connection conn = null;
308+
ResultSet rs = null;
309+
try{
310+
conn = dataSource.getConnection();
311+
rs = conn.createStatement().executeQuery(sql);
312+
if (rs.next()) {
313+
Assertions.assertTrue(true);
314+
} else {
315+
Assertions.fail();
316+
}
317+
} finally {
318+
IOUtil.close(rs, conn);
319+
}
320+
321+
List<LockDO> lockDOs_2 = new ArrayList<>();
322+
for(int i = 0; i < 3; i++) {
323+
LockDO lock = new LockDO();
324+
lock.setResourceId("abc");
325+
lock.setXid("abc-123:333");
326+
lock.setTransactionId(333L);
327+
lock.setBranchId(2L);
328+
lock.setRowKey("test_isLockable_cannot1-"+i);
329+
lock.setPk(String.valueOf(i));
330+
lock.setTableName("t");
331+
lockDOs_2.add(lock);
332+
}
333+
334+
boolean ret2 = dataBaseLockStoreDAO.acquireLock(lockDOs_2, true, true);
335+
Assertions.assertFalse(ret2);
336+
337+
}
338+
281339
@AfterAll
282340
public static void clearStoreDB(){
283341
FileUtils.deleteRecursive("db_store", true);

0 commit comments

Comments
 (0)