From 12363a8e487bf1480dd5da6a14c0b751c9c6dc87 Mon Sep 17 00:00:00 2001 From: changeAtLater <128348688+changeAtLater@users.noreply.github.com> Date: Mon, 9 Sep 2024 11:43:26 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=89=B9=E9=87=8F?= =?UTF-8?q?=E6=8F=92=E5=85=A5=E9=94=81=E6=97=B6=E7=9A=84=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E5=88=A4=E6=96=AD=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../storage/db/lock/LockStoreDataBaseDAO.java | 29 ++++++++++++++----- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/server/src/main/java/org/apache/seata/server/storage/db/lock/LockStoreDataBaseDAO.java b/server/src/main/java/org/apache/seata/server/storage/db/lock/LockStoreDataBaseDAO.java index 3bc6a4e3249..150cb1ab001 100644 --- a/server/src/main/java/org/apache/seata/server/storage/db/lock/LockStoreDataBaseDAO.java +++ b/server/src/main/java/org/apache/seata/server/storage/db/lock/LockStoreDataBaseDAO.java @@ -16,11 +16,7 @@ */ package org.apache.seata.server.storage.db.lock; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.SQLIntegrityConstraintViolationException; +import java.sql.*; import java.util.Collections; import java.util.HashSet; import java.util.List; @@ -372,10 +368,27 @@ protected boolean doAcquireLocks(Connection conn, List 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 (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; + } + while (nextException != null) { + if (!(nextException instanceof SQLIntegrityConstraintViolationException)) { + throw nextException; + } + nextException = nextException.getNextException(); + } return false; - } catch (SQLException e) { - throw e; } finally { IOUtil.close(ps); } From 9e17d404c3e31d9af1c9131cfd401b77336ad1d3 Mon Sep 17 00:00:00 2001 From: changeAtLater <128348688+changeAtLater@users.noreply.github.com> Date: Mon, 9 Sep 2024 11:47:14 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8D=95=E5=85=83?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lock/db/DataBaseLockStoreDAOTest.java | 57 ++++++++++++++++++- 1 file changed, 55 insertions(+), 2 deletions(-) diff --git a/server/src/test/java/org/apache/seata/server/lock/db/DataBaseLockStoreDAOTest.java b/server/src/test/java/org/apache/seata/server/lock/db/DataBaseLockStoreDAOTest.java index 1441f8e634d..baf9fc2e3c6 100644 --- a/server/src/test/java/org/apache/seata/server/lock/db/DataBaseLockStoreDAOTest.java +++ b/server/src/test/java/org/apache/seata/server/lock/db/DataBaseLockStoreDAOTest.java @@ -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; @@ -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); } @@ -278,6 +280,57 @@ public void test_isLockable_cannot() throws SQLException { } + @Test + public void test_isLockable_cannot1() throws SQLException { + List 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 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); From 68fd9155391358c28d9e51d709ec4f0f14b66ff1 Mon Sep 17 00:00:00 2001 From: changeAtLater <128348688+changeAtLater@users.noreply.github.com> Date: Tue, 10 Sep 2024 09:12:34 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E5=81=9C=E7=94=A8*=E5=8F=B7=E5=AF=BC?= =?UTF-8?q?=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../seata/server/storage/db/lock/LockStoreDataBaseDAO.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/server/src/main/java/org/apache/seata/server/storage/db/lock/LockStoreDataBaseDAO.java b/server/src/main/java/org/apache/seata/server/storage/db/lock/LockStoreDataBaseDAO.java index 150cb1ab001..f3846a34bbd 100644 --- a/server/src/main/java/org/apache/seata/server/storage/db/lock/LockStoreDataBaseDAO.java +++ b/server/src/main/java/org/apache/seata/server/storage/db/lock/LockStoreDataBaseDAO.java @@ -16,7 +16,12 @@ */ package org.apache.seata.server.storage.db.lock; -import java.sql.*; +import java.sql.BatchUpdateException; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.SQLIntegrityConstraintViolationException; import java.util.Collections; import java.util.HashSet; import java.util.List; From 4f5a53e98700bb573882909de1e46b77311ba069 Mon Sep 17 00:00:00 2001 From: changeAtLater <128348688+changeAtLater@users.noreply.github.com> Date: Tue, 10 Sep 2024 09:22:41 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E5=AE=8C=E5=96=84=E5=8F=98=E6=9B=B4?= =?UTF-8?q?=E8=AE=B0=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- changes/en-us/2.x.md | 1 + changes/zh-cn/2.x.md | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/changes/en-us/2.x.md b/changes/en-us/2.x.md index 9093aed1db7..742be74bae5 100644 --- a/changes/en-us/2.x.md +++ b/changes/en-us/2.x.md @@ -36,6 +36,7 @@ Add changes here for all PR submitted to the 2.x branch. - [[#6812](https://github.com/apache/incubator-seata/pull/6812)] bugfix: change group and node offline status are not pushed in real time - [[#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 +- [[#6833](https://github.com/apache/incubator-seata/pull/6833)] SQLIntegrityConstraintViolationException capture incorrectly when inserting a globallock diff --git a/changes/zh-cn/2.x.md b/changes/zh-cn/2.x.md index 850f1d7ea74..b7b8e89c22f 100644 --- a/changes/zh-cn/2.x.md +++ b/changes/zh-cn/2.x.md @@ -37,7 +37,10 @@ - [[#6759](https://github.com/apache/incubator-seata/pull/6759)] 修复跨库表主动刷新`tableMeta`的异常问题 - [[#6817](https://github.com/apache/incubator-seata/pull/6817)] 修复namingserver切换事务分组失效的问题 - [[#6820](https://github.com/apache/incubator-seata/pull/6820)] 修复Dockerfile得文件结构错误 -- +- [[#6833](https://github.com/apache/incubator-seata/pull/6833)] 插入全局锁时 SQLIntegrityConstraintViolationException 捕获不正确 + + + ### optimize: - [[#6499](https://github.com/apache/incubator-seata/pull/6499)] 拆分 committing 和 rollbacking 状态的任务线程池 - [[#6208](https://github.com/apache/incubator-seata/pull/6208)] 支持多版本的Seata序列化 From b83dbb5eb8a817d7670b00161cfa7817d015d361 Mon Sep 17 00:00:00 2001 From: changeAtLater <128348688+changeAtLater@users.noreply.github.com> Date: Tue, 10 Sep 2024 09:45:55 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=E8=B0=83=E6=95=B4=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E5=88=A4=E6=96=AD=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/storage/db/lock/LockStoreDataBaseDAO.java | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/server/src/main/java/org/apache/seata/server/storage/db/lock/LockStoreDataBaseDAO.java b/server/src/main/java/org/apache/seata/server/storage/db/lock/LockStoreDataBaseDAO.java index f3846a34bbd..bb8bfd11168 100644 --- a/server/src/main/java/org/apache/seata/server/storage/db/lock/LockStoreDataBaseDAO.java +++ b/server/src/main/java/org/apache/seata/server/storage/db/lock/LockStoreDataBaseDAO.java @@ -386,14 +386,10 @@ protected boolean doAcquireLocks(Connection conn, List lockDOs) throws S SQLException nextException = e.getNextException(); if (nextException == null) { throw e; + } else if (nextException instanceof SQLIntegrityConstraintViolationException) { + return false; } - while (nextException != null) { - if (!(nextException instanceof SQLIntegrityConstraintViolationException)) { - throw nextException; - } - nextException = nextException.getNextException(); - } - return false; + throw nextException; } finally { IOUtil.close(ps); } From 942091a406ae9b5f5d97c102e73a991b66eaddca Mon Sep 17 00:00:00 2001 From: changeAtLater <128348688+changeAtLater@users.noreply.github.com> Date: Wed, 11 Sep 2024 08:53:06 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=E4=BF=AE=E5=A4=8D`DataBaseLockStoreDAOTest?= =?UTF-8?q?`=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lock/db/DataBaseLockStoreDAOTest.java | 31 +++++++++++-------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/server/src/test/java/org/apache/seata/server/lock/db/DataBaseLockStoreDAOTest.java b/server/src/test/java/org/apache/seata/server/lock/db/DataBaseLockStoreDAOTest.java index baf9fc2e3c6..efd44516d2f 100644 --- a/server/src/test/java/org/apache/seata/server/lock/db/DataBaseLockStoreDAOTest.java +++ b/server/src/test/java/org/apache/seata/server/lock/db/DataBaseLockStoreDAOTest.java @@ -90,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); @@ -99,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{ @@ -128,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); @@ -137,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{ @@ -168,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); @@ -177,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{ @@ -216,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); @@ -238,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); @@ -247,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{ @@ -269,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); @@ -289,7 +293,7 @@ public void test_isLockable_cannot1() throws SQLException { lock.setXid("abc-123:222"); lock.setTransactionId(222L); lock.setBranchId(1L); - lock.setRowKey("abc-"+i); + lock.setRowKey("test_isLockable_cannot1-"+i); lock.setPk(String.valueOf(i)); lock.setTableName("t"); lockDOs.add(lock); @@ -298,7 +302,8 @@ public void test_isLockable_cannot1() throws SQLException { 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')" ; + 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{ @@ -320,7 +325,7 @@ public void test_isLockable_cannot1() throws SQLException { lock.setXid("abc-123:333"); lock.setTransactionId(333L); lock.setBranchId(2L); - lock.setRowKey("abc-"+i); + lock.setRowKey("test_isLockable_cannot1-"+i); lock.setPk(String.valueOf(i)); lock.setTableName("t"); lockDOs_2.add(lock);