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 de7547f2469 Refactor transaction connection closing logic (#37211)
de7547f2469 is described below

commit de7547f2469a1f1ef78179e27163e6603dc3cc6a
Author: ZhangCheng <[email protected]>
AuthorDate: Fri Nov 28 17:10:49 2025 +0800

    Refactor transaction connection closing logic (#37211)
    
    * Refactor transaction connection closing logic
    
    * Refactor transaction connection closing logic
---
 .../driver/jdbc/core/connection/DriverDatabaseConnectionManager.java  | 4 +++-
 .../connector/jdbc/transaction/ProxyBackendTransactionManager.java    | 4 ++--
 .../jdbc/transaction/ProxyBackendTransactionManagerTest.java          | 4 ++--
 3 files changed, 7 insertions(+), 5 deletions(-)

diff --git 
a/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManager.java
 
b/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManager.java
index 9ce256e950a..ed3e8830307 100644
--- 
a/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManager.java
+++ 
b/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManager.java
@@ -130,6 +130,9 @@ public final class DriverDatabaseConnectionManager 
implements DatabaseConnection
     public void begin() throws SQLException {
         ConnectionTransaction connectionTransaction = 
getConnectionTransaction();
         
connectionContext.getTransactionContext().beginTransaction(connectionTransaction.getTransactionType().name(),
 connectionTransaction.getDistributedTransactionManager());
+        if (!connectionTransaction.isLocalTransaction()) {
+            close();
+        }
         doBegin(connectionTransaction);
     }
     
@@ -137,7 +140,6 @@ public final class DriverDatabaseConnectionManager 
implements DatabaseConnection
         if (connectionTransaction.isLocalTransaction()) {
             setAutoCommit(false);
         } else {
-            close();
             connectionTransaction.begin();
         }
     }
diff --git 
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/transaction/ProxyBackendTransactionManager.java
 
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/transaction/ProxyBackendTransactionManager.java
index 8ffff3feddc..673ac61e359 100644
--- 
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/transaction/ProxyBackendTransactionManager.java
+++ 
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/transaction/ProxyBackendTransactionManager.java
@@ -77,6 +77,8 @@ public final class ProxyBackendTransactionManager {
      */
     public void begin() {
         if 
(!connection.getConnectionSession().getTransactionStatus().isInTransaction()) {
+            connection.closeHandlers(true);
+            connection.closeConnections(false);
             
connection.getConnectionSession().getTransactionStatus().setInTransaction(true);
             transactionContext.beginTransaction(transactionType.name(), 
distributedTransactionManager);
         }
@@ -84,8 +86,6 @@ public final class ProxyBackendTransactionManager {
     }
     
     private void doBegin() {
-        connection.closeHandlers(true);
-        connection.closeConnections(false);
         DatabaseType databaseType = 
ProxyContext.getInstance().getContextManager().getDatabaseType();
         for (Entry<ShardingSphereRule, TransactionHook> entry : 
transactionHooks.entrySet()) {
             entry.getValue().beforeBegin(entry.getKey(), databaseType, 
transactionContext);
diff --git 
a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/transaction/ProxyBackendTransactionManagerTest.java
 
b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/transaction/ProxyBackendTransactionManagerTest.java
index 355dd2e61d1..ffad8752776 100644
--- 
a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/transaction/ProxyBackendTransactionManagerTest.java
+++ 
b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/transaction/ProxyBackendTransactionManagerTest.java
@@ -111,9 +111,9 @@ class ProxyBackendTransactionManagerTest {
     void assertBeginForDistributedTransaction() {
         ContextManager contextManager = mockContextManager(TransactionType.XA);
         
when(ProxyContext.getInstance().getContextManager()).thenReturn(contextManager);
-        newTransactionManager(TransactionType.XA, true);
+        newTransactionManager(TransactionType.XA, false);
         transactionManager.begin();
-        verify(transactionStatus, never()).setInTransaction(true);
+        verify(transactionStatus, times(1)).setInTransaction(true);
         verify(databaseConnectionManager, times(1)).closeConnections(false);
         verify(distributedTransactionManager).begin();
     }

Reply via email to