You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2024/06/17 07:41:22 UTC

(shardingsphere) branch master updated: Refactor DriverDatabaseConnectionManager (#31730)

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 8c4597ba0ab Refactor DriverDatabaseConnectionManager (#31730)
8c4597ba0ab is described below

commit 8c4597ba0abdea275103dd40dfbb361c8c728f27
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Mon Jun 17 15:41:16 2024 +0800

    Refactor DriverDatabaseConnectionManager (#31730)
---
 .../DriverDatabaseConnectionManager.java           | 26 +++++++++++++---------
 1 file changed, 16 insertions(+), 10 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 ae2a8a21789..2aede3b707a 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
@@ -64,12 +64,9 @@ import java.util.concurrent.ThreadLocalRandom;
  */
 public final class DriverDatabaseConnectionManager implements OnlineDatabaseConnectionManager<Connection>, AutoCloseable {
     
-    private final ContextManager contextManager;
-    
     private final String databaseName;
     
-    @Getter
-    private final ConnectionContext connectionContext;
+    private final ContextManager contextManager;
     
     private final Map<String, DataSource> physicalDataSourceMap;
     
@@ -77,6 +74,9 @@ public final class DriverDatabaseConnectionManager implements OnlineDatabaseConn
     
     private final Map<String, DataSource> dataSourceMap;
     
+    @Getter
+    private final ConnectionContext connectionContext;
+    
     private final Multimap<String, Connection> cachedConnections = LinkedHashMultimap.create();
     
     private final MethodInvocationRecorder<Connection> methodInvocationRecorder = new MethodInvocationRecorder<>();
@@ -84,15 +84,13 @@ public final class DriverDatabaseConnectionManager implements OnlineDatabaseConn
     private final ForceExecuteTemplate<Connection> forceExecuteTemplate = new ForceExecuteTemplate<>();
     
     public DriverDatabaseConnectionManager(final String databaseName, final ContextManager contextManager) {
-        this.contextManager = contextManager;
         this.databaseName = databaseName;
-        connectionContext = new ConnectionContext(cachedConnections::keySet);
-        connectionContext.setCurrentDatabase(databaseName);
+        this.contextManager = contextManager;
         physicalDataSourceMap = getPhysicalDataSourceMap(databaseName, contextManager);
         trafficDataSourceMap = getTrafficDataSourceMap(databaseName, contextManager);
-        dataSourceMap = new LinkedHashMap<>(physicalDataSourceMap.size() + trafficDataSourceMap.size(), 1F);
-        dataSourceMap.putAll(physicalDataSourceMap);
-        dataSourceMap.putAll(trafficDataSourceMap);
+        dataSourceMap = getDataSourceMap();
+        connectionContext = new ConnectionContext(cachedConnections::keySet);
+        connectionContext.setCurrentDatabase(databaseName);
     }
     
     private Map<String, DataSource> getPhysicalDataSourceMap(final String databaseName, final ContextManager contextManager) {
@@ -150,6 +148,14 @@ public final class DriverDatabaseConnectionManager implements OnlineDatabaseConn
         return String.format("%s//%s:%s/%s%s", jdbcUrlPrefix, instanceMetaData.getIp(), instanceMetaData.getPort(), schema, jdbcUrlSuffix);
     }
     
+    private Map<String, DataSource> getDataSourceMap() {
+        Map<String, DataSource> result;
+        result = new LinkedHashMap<>(physicalDataSourceMap.size() + trafficDataSourceMap.size(), 1F);
+        result.putAll(physicalDataSourceMap);
+        result.putAll(trafficDataSourceMap);
+        return result;
+    }
+    
     /**
      * Get connection transaction.
      *