You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by ch...@apache.org on 2024/06/16 05:56:15 UTC

(shardingsphere) branch master updated: Extract the usage of cachedConnections.values (#31714)

This is an automated email from the ASF dual-hosted git repository.

chengzhang 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 eba583b4585 Extract the usage of cachedConnections.values (#31714)
eba583b4585 is described below

commit eba583b4585914af55861bce801ef19dab952e11
Author: ZhangCheng <ch...@apache.org>
AuthorDate: Sun Jun 16 13:56:08 2024 +0800

    Extract the usage of cachedConnections.values (#31714)
---
 .../DriverDatabaseConnectionManager.java           | 24 +++++++++++++---------
 .../core/connection/ShardingSphereConnection.java  | 12 ++++++++---
 2 files changed, 23 insertions(+), 13 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 1ef2174e108..680e15abea9 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
@@ -169,7 +169,11 @@ public final class DriverDatabaseConnectionManager implements OnlineDatabaseConn
      */
     public void setAutoCommit(final boolean autoCommit) throws SQLException {
         methodInvocationRecorder.record("setAutoCommit", target -> target.setAutoCommit(autoCommit));
-        forceExecuteTemplate.execute(cachedConnections.values(), connection -> connection.setAutoCommit(autoCommit));
+        forceExecuteTemplate.execute(getCachedConnections(), connection -> connection.setAutoCommit(autoCommit));
+    }
+    
+    private Collection<Connection> getCachedConnections() {
+        return cachedConnections.values();
     }
     
     /**
@@ -181,14 +185,14 @@ public final class DriverDatabaseConnectionManager implements OnlineDatabaseConn
         ConnectionTransaction connectionTransaction = getConnectionTransaction();
         try {
             if (connectionTransaction.isLocalTransaction() && connectionContext.getTransactionContext().isExceptionOccur()) {
-                forceExecuteTemplate.execute(cachedConnections.values(), Connection::rollback);
+                forceExecuteTemplate.execute(getCachedConnections(), Connection::rollback);
             } else if (connectionTransaction.isLocalTransaction()) {
-                forceExecuteTemplate.execute(cachedConnections.values(), Connection::commit);
+                forceExecuteTemplate.execute(getCachedConnections(), Connection::commit);
             } else {
                 connectionTransaction.commit();
             }
         } finally {
-            for (Connection each : cachedConnections.values()) {
+            for (Connection each : getCachedConnections()) {
                 ConnectionSavepointManager.getInstance().transactionFinished(each);
             }
         }
@@ -203,12 +207,12 @@ public final class DriverDatabaseConnectionManager implements OnlineDatabaseConn
         ConnectionTransaction connectionTransaction = getConnectionTransaction();
         try {
             if (connectionTransaction.isLocalTransaction()) {
-                forceExecuteTemplate.execute(cachedConnections.values(), Connection::rollback);
+                forceExecuteTemplate.execute(getCachedConnections(), Connection::rollback);
             } else {
                 connectionTransaction.rollback();
             }
         } finally {
-            for (Connection each : cachedConnections.values()) {
+            for (Connection each : getCachedConnections()) {
                 ConnectionSavepointManager.getInstance().transactionFinished(each);
             }
         }
@@ -221,7 +225,7 @@ public final class DriverDatabaseConnectionManager implements OnlineDatabaseConn
      * @throws SQLException SQL exception
      */
     public void rollback(final Savepoint savepoint) throws SQLException {
-        for (Connection each : cachedConnections.values()) {
+        for (Connection each : getCachedConnections()) {
             ConnectionSavepointManager.getInstance().rollbackToSavepoint(each, savepoint.getSavepointName());
         }
     }
@@ -235,7 +239,7 @@ public final class DriverDatabaseConnectionManager implements OnlineDatabaseConn
      */
     public Savepoint setSavepoint(final String savepointName) throws SQLException {
         ShardingSphereSavepoint result = new ShardingSphereSavepoint(savepointName);
-        for (Connection each : cachedConnections.values()) {
+        for (Connection each : getCachedConnections()) {
             ConnectionSavepointManager.getInstance().setSavepoint(each, savepointName);
         }
         methodInvocationRecorder.record("setSavepoint", target -> ConnectionSavepointManager.getInstance().setSavepoint(target, savepointName));
@@ -250,7 +254,7 @@ public final class DriverDatabaseConnectionManager implements OnlineDatabaseConn
      */
     public Savepoint setSavepoint() throws SQLException {
         ShardingSphereSavepoint result = new ShardingSphereSavepoint();
-        for (Connection each : cachedConnections.values()) {
+        for (Connection each : getCachedConnections()) {
             ConnectionSavepointManager.getInstance().setSavepoint(each, result.getSavepointName());
         }
         methodInvocationRecorder.record("setSavepoint", target -> ConnectionSavepointManager.getInstance().setSavepoint(target, result.getSavepointName()));
@@ -264,7 +268,7 @@ public final class DriverDatabaseConnectionManager implements OnlineDatabaseConn
      * @throws SQLException SQL exception
      */
     public void releaseSavepoint(final Savepoint savepoint) throws SQLException {
-        for (Connection each : cachedConnections.values()) {
+        for (Connection each : getCachedConnections()) {
             ConnectionSavepointManager.getInstance().releaseSavepoint(each, savepoint.getSavepointName());
         }
     }
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 9e6caad1a03..7b1683a923f 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
@@ -184,9 +184,15 @@ public final class ShardingSphereConnection extends AbstractConnectionAdapter {
     
     private void processLocalTransaction() throws SQLException {
         databaseConnectionManager.setAutoCommit(autoCommit);
-        if (!autoCommit) {
-            getConnectionContext().getTransactionContext()
-                    .beginTransaction(String.valueOf(contextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getSingleRule(TransactionRule.class).getDefaultType()));
+        if (autoCommit) {
+            if (getConnectionContext().getTransactionContext().isInTransaction()) {
+                getConnectionContext().getTransactionContext().close();
+            }
+        } else {
+            if (!getConnectionContext().getTransactionContext().isInTransaction()) {
+                getConnectionContext().getTransactionContext()
+                        .beginTransaction(String.valueOf(contextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getSingleRule(TransactionRule.class).getDefaultType()));
+            }
         }
     }