This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 8a9f6dc42fd Fix transaction context not cleaned up when xa transaction
is committed (#31740)
8a9f6dc42fd is described below
commit 8a9f6dc42fd44a4eb53a23c54620f2ffc5d29722
Author: ZhangCheng <[email protected]>
AuthorDate: Tue Jun 18 12:51:33 2024 +0800
Fix transaction context not cleaned up when xa transaction is committed
(#31740)
* Fix transaction context not cleaned up when xa transaction is committed
* Fix transaction context not cleaned up when xa transaction is committed
---
.../jdbc/core/connection/ShardingSphereConnection.java | 18 ++++++++----------
.../core/connection/ShardingSphereConnectionTest.java | 4 ++--
2 files changed, 10 insertions(+), 12 deletions(-)
diff --git
a/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/ShardingSphereConnection.java
b/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/ShardingSphereConnection.java
index a3e7259970a..457bf0e654e 100644
---
a/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/ShardingSphereConnection.java
+++
b/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/ShardingSphereConnection.java
@@ -198,15 +198,13 @@ public final class ShardingSphereConnection extends
AbstractConnectionAdapter {
private void processLocalTransaction() throws SQLException {
databaseConnectionManager.setAutoCommit(autoCommit);
- if (autoCommit) {
- if
(databaseConnectionManager.getConnectionContext().getTransactionContext().isInTransaction())
{
-
databaseConnectionManager.getConnectionContext().getTransactionContext().close();
- }
- } else {
- if
(!databaseConnectionManager.getConnectionContext().getTransactionContext().isInTransaction())
{
-
databaseConnectionManager.getConnectionContext().getTransactionContext()
-
.beginTransaction(String.valueOf(contextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getSingleRule(TransactionRule.class).getDefaultType()));
- }
+ if (autoCommit &&
databaseConnectionManager.getConnectionContext().getTransactionContext().isInTransaction())
{
+
databaseConnectionManager.getConnectionContext().getTransactionContext().close();
+ return;
+ }
+ if (!autoCommit &&
!databaseConnectionManager.getConnectionContext().getTransactionContext().isInTransaction())
{
+
databaseConnectionManager.getConnectionContext().getTransactionContext()
+
.beginTransaction(String.valueOf(contextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getSingleRule(TransactionRule.class).getDefaultType()));
}
}
@@ -216,7 +214,7 @@ public final class ShardingSphereConnection extends
AbstractConnectionAdapter {
beginDistributedTransaction();
break;
case COMMIT:
- databaseConnectionManager.getConnectionTransaction().commit();
+ commit();
break;
default:
break;
diff --git
a/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/ShardingSphereConnectionTest.java
b/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/ShardingSphereConnectionTest.java
index c1fc69673a6..50abb117595 100644
---
a/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/ShardingSphereConnectionTest.java
+++
b/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/ShardingSphereConnectionTest.java
@@ -70,11 +70,11 @@ class ShardingSphereConnectionTest {
when(connectionTransaction.getDistributedTransactionOperationType(true)).thenReturn(DistributedTransactionOperationType.COMMIT);
when(connectionTransaction.getTransactionType()).thenReturn(TransactionType.XA);
try (ShardingSphereConnection connection = new
ShardingSphereConnection(DefaultDatabase.LOGIC_NAME, mockContextManager())) {
- mockConnectionManager(connection, connectionTransaction);
+ DriverDatabaseConnectionManager connectionManager =
mockConnectionManager(connection, connectionTransaction);
connection.setAutoCommit(true);
assertTrue(connection.getAutoCommit());
+ verify(connectionManager).commit();
}
- verify(connectionTransaction).commit();
}
@Test