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

zhangliang 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 1adda16c137 Cross-library data source confusion repair (#19939)
1adda16c137 is described below

commit 1adda16c1376f9f5ae4a35f95ca038b9d8831804
Author: natehuang <[email protected]>
AuthorDate: Mon Aug 8 00:23:32 2022 +0800

    Cross-library data source confusion repair (#19939)
---
 .../communication/jdbc/connection/JDBCBackendConnection.java      | 8 ++++----
 .../proxy/backend/communication/vertx/VertxBackendConnection.java | 6 +++---
 .../communication/jdbc/connection/JDBCBackendConnectionTest.java  | 2 +-
 .../backend/communication/jdbc/connection/MockConnectionUtil.java | 2 +-
 4 files changed, 9 insertions(+), 9 deletions(-)

diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/JDBCBackendConnection.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/JDBCBackendConnection.java
index 19cb274b5ae..377c82ea33e 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/JDBCBackendConnection.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/JDBCBackendConnection.java
@@ -67,9 +67,10 @@ public final class JDBCBackendConnection implements 
BackendConnection<Void>, Exe
     
     @Override
     public List<Connection> getConnections(final String dataSourceName, final 
int connectionSize, final ConnectionMode connectionMode) throws SQLException {
+        Preconditions.checkNotNull(connectionSession.getDatabaseName(), 
"Current database name is null.");
         Collection<Connection> connections;
         synchronized (cachedConnections) {
-            connections = cachedConnections.get(dataSourceName);
+            connections = 
cachedConnections.get(connectionSession.getDatabaseName() + "." + 
dataSourceName);
         }
         List<Connection> result;
         if (connections.size() >= connectionSize) {
@@ -80,19 +81,18 @@ public final class JDBCBackendConnection implements 
BackendConnection<Void>, Exe
             List<Connection> newConnections = 
createNewConnections(dataSourceName, connectionSize - connections.size(), 
connectionMode);
             result.addAll(newConnections);
             synchronized (cachedConnections) {
-                cachedConnections.putAll(dataSourceName, newConnections);
+                cachedConnections.putAll(connectionSession.getDatabaseName() + 
"." + dataSourceName, newConnections);
             }
         } else {
             result = createNewConnections(dataSourceName, connectionSize, 
connectionMode);
             synchronized (cachedConnections) {
-                cachedConnections.putAll(dataSourceName, result);
+                cachedConnections.putAll(connectionSession.getDatabaseName() + 
"." + dataSourceName, result);
             }
         }
         return result;
     }
     
     private List<Connection> createNewConnections(final String dataSourceName, 
final int connectionSize, final ConnectionMode connectionMode) throws 
SQLException {
-        Preconditions.checkNotNull(connectionSession.getDatabaseName(), 
"Current schema is null.");
         List<Connection> result = 
ProxyContext.getInstance().getBackendDataSource().getConnections(connectionSession.getDatabaseName(),
 dataSourceName, connectionSize, connectionMode);
         setSessionVariablesIfNecessary(result);
         for (Connection each : result) {
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/vertx/VertxBackendConnection.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/vertx/VertxBackendConnection.java
index 634cdbb732e..d8d823ddb5b 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/vertx/VertxBackendConnection.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/vertx/VertxBackendConnection.java
@@ -73,7 +73,7 @@ public final class VertxBackendConnection implements 
BackendConnection<Future<Vo
     private List<Future<? extends SqlClient>> 
getConnectionsWithTransaction(final String dataSourceName, final int 
connectionSize) {
         Collection<Future<SqlConnection>> connections;
         synchronized (cachedConnections) {
-            connections = cachedConnections.get(dataSourceName);
+            connections = 
cachedConnections.get(connectionSession.getDatabaseName() + "." + 
dataSourceName);
         }
         List<Future<SqlConnection>> result;
         if (connections.size() >= connectionSize) {
@@ -84,12 +84,12 @@ public final class VertxBackendConnection implements 
BackendConnection<Future<Vo
             List<Future<SqlConnection>> newConnections = 
createNewConnections(dataSourceName, connectionSize - connections.size());
             result.addAll(newConnections);
             synchronized (cachedConnections) {
-                cachedConnections.putAll(dataSourceName, newConnections);
+                cachedConnections.putAll(connectionSession.getDatabaseName() + 
"." + dataSourceName, newConnections);
             }
         } else {
             result = createNewConnections(dataSourceName, connectionSize);
             synchronized (cachedConnections) {
-                cachedConnections.putAll(dataSourceName, result);
+                cachedConnections.putAll(connectionSession.getDatabaseName() + 
"." + dataSourceName, result);
             }
         }
         return new ArrayList<>(result);
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/JDBCBackendConnectionTest.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/JDBCBackendConnectionTest.java
index 9c1b9bcd5c0..1b3207bfa6f 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/JDBCBackendConnectionTest.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/JDBCBackendConnectionTest.java
@@ -316,7 +316,7 @@ public final class JDBCBackendConnectionTest extends 
ProxyContextRestorer {
         List<Connection> fetchedConnections = 
backendConnection.getConnections("ds1", 1, null);
         assertThat(fetchedConnections.size(), is(1));
         assertTrue(fetchedConnections.contains(connections.get(0)));
-        assertConnectionsCached("ds1", connections);
+        assertConnectionsCached(connectionSession.getDatabaseName() + ".ds1", 
connections);
     }
     
     @SuppressWarnings("unchecked")
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/MockConnectionUtil.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/MockConnectionUtil.java
index e11e683e4f7..256f4b459c2 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/MockConnectionUtil.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/MockConnectionUtil.java
@@ -46,7 +46,7 @@ final class MockConnectionUtil {
     @SneakyThrows(ReflectiveOperationException.class)
     static void setCachedConnections(final JDBCBackendConnection 
backendConnection, final String dataSourceName, final int connectionSize) {
         Multimap<String, Connection> cachedConnections = HashMultimap.create();
-        cachedConnections.putAll(dataSourceName, 
mockNewConnections(connectionSize));
+        
cachedConnections.putAll(backendConnection.getConnectionSession().getDatabaseName()
 + "." + dataSourceName, mockNewConnections(connectionSize));
         Field field = 
backendConnection.getClass().getDeclaredField("cachedConnections");
         field.setAccessible(true);
         field.set(backendConnection, cachedConnections);

Reply via email to