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

Conversation

changeAtLater
Copy link
Contributor

  • I have registered the PR changes.

Ⅰ. Describe what this PR did

Ⅱ. Does this pull request fix one issue?

fixes #6822

Ⅲ. Why don't you add test cases (unit test/integration test)?

Ⅳ. Describe how to verify it

Ⅴ. Special notes for reviews

@funky-eyes funky-eyes changed the title 完善批量插入锁时的异常判断逻辑 bugfix: SQLIntegrityConstraintViolationException capture incorrectly when inserting a globallock Sep 9, 2024
Copy link
Contributor

@funky-eyes funky-eyes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/apache/incubator-seata/blob/2.x/changes/zh-cn/2.x.md
https://github.com/apache/incubator-seata/blob/2.x/changes/en-us/2.x.md
请在上述md文件中登记pr信息和作者信息
Please register pr information and author information in the above md file

@funky-eyes funky-eyes added this to the 2.2.0 milestone Sep 9, 2024
@funky-eyes funky-eyes added type: bug Category issues or prs related to bug. first-time contributor first-time contributor mode: AT AT transaction mode module/server server module labels Sep 9, 2024
@funky-eyes
Copy link
Contributor

Caused by: java.sql.BatchUpdateException: Duplicate entry 'jdbc:mysql://127.0.0.1:3306/oa^^^sys_user_role^^^10000002_3' for key 'lock_table.PRIMARY'
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_231]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[na:1.8.0_231]
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[na:1.8.0_231]
        at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[na:1.8.0_231]
        at com.mysql.cj.util.Util.handleNewInstance(Util.java:192) ~[mysql-connector-java-8.0.27.jar:8.0.27]
        at com.mysql.cj.util.Util.getInstance(Util.java:167) ~[mysql-connector-java-8.0.27.jar:8.0.27]
        at com.mysql.cj.util.Util.getInstance(Util.java:174) ~[mysql-connector-java-8.0.27.jar:8.0.27]
        at com.mysql.cj.jdbc.exceptions.SQLError.createBatchUpdateException(SQLError.java:224) ~[mysql-connector-java-8.0.27.jar:8.0.27]
        at com.mysql.cj.jdbc.ClientPreparedStatement.executeBatchedInserts(ClientPreparedStatement.java:755) ~[mysql-connector-java-8.0.27.jar:8.0.27]
        at com.mysql.cj.jdbc.ClientPreparedStatement.executeBatchInternal(ClientPreparedStatement.java:426) ~[mysql-connector-java-8.0.27.jar:8.0.27]
        at com.mysql.cj.jdbc.StatementImpl.executeBatch(StatementImpl.java:800) ~[mysql-connector-java-8.0.27.jar:8.0.27]
        at com.alibaba.druid.pool.DruidPooledPreparedStatement.executeBatch(DruidPooledPreparedStatement.java:565) ~[druid-1.2.6.jar:1.2.6]
        at io.seata.server.storage.db.lock.LockStoreDataBaseDAO.doAcquireLocks(LockStoreDataBaseDAO.java:371) ~[classes!/:1.7.0]
        at io.seata.server.storage.db.lock.LockStoreDataBaseDAO.acquireLock(LockStoreDataBaseDAO.java:187) ~[classes!/:1.7.0]
        ... 24 common frames omitted
Caused by: java.sql.SQLIntegrityConstraintViolationException: Duplicate entry 'jdbc:mysql://127.0.0.1:3306/oa^^^sys_user_role^^^10000002_3' for key 'lock_table.PRIMARY'
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:117) ~[mysql-connector-java-8.0.27.jar:8.0.27]
        at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) ~[mysql-connector-java-8.0.27.jar:8.0.27]
        at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:953) ~[mysql-connector-java-8.0.27.jar:8.0.27]
        at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1098) ~[mysql-connector-java-8.0.27.jar:8.0.27]
        at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1046) ~[mysql-connector-java-8.0.27.jar:8.0.27]
        at com.mysql.cj.jdbc.ClientPreparedStatement.executeLargeUpdate(ClientPreparedStatement.java:1371) ~[mysql-connector-java-8.0.27.jar:8.0.27]
        at com.mysql.cj.jdbc.ClientPreparedStatement.executeBatchedInserts(ClientPreparedStatement.java:716) ~[mysql-connector-java-8.0.27.jar:8.0.27]
        ... 29 common frames omitted
<==

根据这个堆栈,其实可以看到批量插入的时候,nextException是SQLIntegrityConstraintViolationException,如果存在其他异常,可能是数据库之类的异常,这种情况应该属于sqlexception,也不需要额外处理,直接抛出去即可

@changeAtLater
Copy link
Contributor Author

Caused by: java.sql.BatchUpdateException: Duplicate entry 'jdbc:mysql://127.0.0.1:3306/oa^^^sys_user_role^^^10000002_3' for key 'lock_table.PRIMARY'
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_231]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[na:1.8.0_231]
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[na:1.8.0_231]
        at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[na:1.8.0_231]
        at com.mysql.cj.util.Util.handleNewInstance(Util.java:192) ~[mysql-connector-java-8.0.27.jar:8.0.27]
        at com.mysql.cj.util.Util.getInstance(Util.java:167) ~[mysql-connector-java-8.0.27.jar:8.0.27]
        at com.mysql.cj.util.Util.getInstance(Util.java:174) ~[mysql-connector-java-8.0.27.jar:8.0.27]
        at com.mysql.cj.jdbc.exceptions.SQLError.createBatchUpdateException(SQLError.java:224) ~[mysql-connector-java-8.0.27.jar:8.0.27]
        at com.mysql.cj.jdbc.ClientPreparedStatement.executeBatchedInserts(ClientPreparedStatement.java:755) ~[mysql-connector-java-8.0.27.jar:8.0.27]
        at com.mysql.cj.jdbc.ClientPreparedStatement.executeBatchInternal(ClientPreparedStatement.java:426) ~[mysql-connector-java-8.0.27.jar:8.0.27]
        at com.mysql.cj.jdbc.StatementImpl.executeBatch(StatementImpl.java:800) ~[mysql-connector-java-8.0.27.jar:8.0.27]
        at com.alibaba.druid.pool.DruidPooledPreparedStatement.executeBatch(DruidPooledPreparedStatement.java:565) ~[druid-1.2.6.jar:1.2.6]
        at io.seata.server.storage.db.lock.LockStoreDataBaseDAO.doAcquireLocks(LockStoreDataBaseDAO.java:371) ~[classes!/:1.7.0]
        at io.seata.server.storage.db.lock.LockStoreDataBaseDAO.acquireLock(LockStoreDataBaseDAO.java:187) ~[classes!/:1.7.0]
        ... 24 common frames omitted
Caused by: java.sql.SQLIntegrityConstraintViolationException: Duplicate entry 'jdbc:mysql://127.0.0.1:3306/oa^^^sys_user_role^^^10000002_3' for key 'lock_table.PRIMARY'
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:117) ~[mysql-connector-java-8.0.27.jar:8.0.27]
        at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) ~[mysql-connector-java-8.0.27.jar:8.0.27]
        at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:953) ~[mysql-connector-java-8.0.27.jar:8.0.27]
        at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1098) ~[mysql-connector-java-8.0.27.jar:8.0.27]
        at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1046) ~[mysql-connector-java-8.0.27.jar:8.0.27]
        at com.mysql.cj.jdbc.ClientPreparedStatement.executeLargeUpdate(ClientPreparedStatement.java:1371) ~[mysql-connector-java-8.0.27.jar:8.0.27]
        at com.mysql.cj.jdbc.ClientPreparedStatement.executeBatchedInserts(ClientPreparedStatement.java:716) ~[mysql-connector-java-8.0.27.jar:8.0.27]
        ... 29 common frames omitted
<==

根据这个堆栈,其实可以看到批量插入的时候,nextException是SQLIntegrityConstraintViolationException,如果存在其他异常,可能是数据库之类的异常,这种情况应该属于sqlexception,也不需要额外处理,直接抛出去即可

我用mysql测试时,SQLIntegrityConstraintViolationException在cause中, 但我写单元测试时发现, h2的SQLIntegrityConstraintViolationException在nextException中

@changeAtLater
Copy link
Contributor Author

nextException并不会在日志中打印出来,因此我最后选择抛出nextException,可以更好的知道是什么异常

@funky-eyes
Copy link
Contributor

Error: Failures:
Error: DataBaseLockStoreDAOTest.tes_unLocks:178 expected: but was:
Error: DataBaseLockStoreDAOTest.test_acquireLocks:100 expected: but was:
Error: DataBaseLockStoreDAOTest.test_isLockable_can:226 expected: but was:
Error: DataBaseLockStoreDAOTest.test_re_acquireLocks:138 expected: but was:
[INFO]
Error: Tests run: 184, Failures: 4, Errors: 0, Skipped: 3

Copy link

codecov bot commented Sep 11, 2024

Codecov Report

Attention: Patch coverage is 33.33333% with 8 lines in your changes missing coverage. Please review.

Project coverage is 52.43%. Comparing base (fde59c1) to head (942091a).
Report is 1 commits behind head on 2.x.

Files with missing lines Patch % Lines
...a/server/storage/db/lock/LockStoreDataBaseDAO.java 33.33% 5 Missing and 3 partials ⚠️
Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff              @@
##                2.x    #6833      +/-   ##
============================================
+ Coverage     52.41%   52.43%   +0.02%     
- Complexity     6379     6384       +5     
============================================
  Files          1080     1080              
  Lines         37565    37575      +10     
  Branches       4451     4457       +6     
============================================
+ Hits          19691    19704      +13     
+ Misses        15923    15920       -3     
  Partials       1951     1951              
Files with missing lines Coverage Δ
...a/server/storage/db/lock/LockStoreDataBaseDAO.java 52.77% <33.33%> (+2.29%) ⬆️

... and 3 files with indirect coverage changes

Copy link
Contributor

@funky-eyes funky-eyes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@funky-eyes funky-eyes merged commit b2f9c3a into apache:2.x Sep 11, 2024
7 checks passed
@funky-eyes
Copy link
Contributor

Thank you for your contributions to Seata. If possible, please send your Dingtalk contact information to my email, and I will invite you to join the Seata developer group.
jianbin@apache.org

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
first-time contributor first-time contributor mode: AT AT transaction mode module/server server module type: bug Category issues or prs related to bug.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

已经关闭的问题又出现了:Duplicate entry for key 'lock_table.PRIMARY'
2 participants