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 ae1cc5ef18f Refactor 
DriverDatabaseConnectionManager#getRandomConnection (#28415)
ae1cc5ef18f is described below

commit ae1cc5ef18f821294c2dde0cbebf7cb75e86047f
Author: ZhangCheng <[email protected]>
AuthorDate: Tue Sep 12 10:30:33 2023 +0800

    Refactor DriverDatabaseConnectionManager#getRandomConnection (#28415)
---
 .../core/connection/DriverDatabaseConnectionManager.java  | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git 
a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManager.java
 
b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManager.java
index c0cbf83a006..c1f56fc7f2c 100644
--- 
a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManager.java
+++ 
b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManager.java
@@ -313,9 +313,13 @@ public final class DriverDatabaseConnectionManager 
implements DatabaseConnection
      * @return random physical data source name
      */
     public String getRandomPhysicalDataSourceName() {
+        return getRandomPhysicalDatabaseAndDataSourceName()[1];
+    }
+    
+    private String[] getRandomPhysicalDatabaseAndDataSourceName() {
         Collection<String> cachedPhysicalDataSourceNames = 
Sets.intersection(physicalDataSourceMap.keySet(), cachedConnections.keySet());
         Collection<String> databaseAndDatasourceNames = 
cachedPhysicalDataSourceNames.isEmpty() ? physicalDataSourceMap.keySet() : 
cachedPhysicalDataSourceNames;
-        return new 
ArrayList<>(databaseAndDatasourceNames).get(random.nextInt(databaseAndDatasourceNames.size())).split("\\.")[1];
+        return new 
ArrayList<>(databaseAndDatasourceNames).get(random.nextInt(databaseAndDatasourceNames.size())).split("\\.");
     }
     
     /**
@@ -325,12 +329,17 @@ public final class DriverDatabaseConnectionManager 
implements DatabaseConnection
      * @throws SQLException SQL exception
      */
     public Connection getRandomConnection() throws SQLException {
-        return getConnections(getRandomPhysicalDataSourceName(), 0, 1, 
ConnectionMode.MEMORY_STRICTLY).get(0);
+        String[] databaseAndDataSourceName = 
getRandomPhysicalDatabaseAndDataSourceName();
+        return getConnections(databaseAndDataSourceName[0], 
databaseAndDataSourceName[1], 0, 1, ConnectionMode.MEMORY_STRICTLY).get(0);
     }
     
     @Override
     public List<Connection> getConnections(final String dataSourceName, final 
int connectionOffset, final int connectionSize, final ConnectionMode 
connectionMode) throws SQLException {
-        String currentDatabaseName = 
connectionContext.getDatabaseName().orElse(databaseName);
+        return 
getConnections(connectionContext.getDatabaseName().orElse(databaseName), 
dataSourceName, connectionOffset, connectionSize, connectionMode);
+    }
+    
+    private List<Connection> getConnections(final String currentDatabaseName, 
final String dataSourceName, final int connectionOffset, final int 
connectionSize,
+                                            final ConnectionMode 
connectionMode) throws SQLException {
         String cacheKey = getKey(currentDatabaseName, dataSourceName);
         DataSource dataSource = databaseName.equals(currentDatabaseName) ? 
dataSourceMap.get(cacheKey) : 
contextManager.getStorageUnits(currentDatabaseName).get(dataSourceName).getDataSource();
         Preconditions.checkNotNull(dataSource, "Missing the data source name: 
'%s'", dataSourceName);

Reply via email to