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

menghaoran 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 d02698a  Fix exception when execute select statement with traffic rule 
(#15425)
d02698a is described below

commit d02698ae25fd81dc384d58a729dbf51a300576cb
Author: Zhengqiang Duan <[email protected]>
AuthorDate: Wed Feb 16 08:23:19 2022 +0800

    Fix exception when execute select statement with traffic rule (#15425)
---
 .../driver/jdbc/core/connection/ConnectionManager.java           | 9 +++------
 .../driver/jdbc/core/connection/ConnectionManagerTest.java       | 8 ++++++--
 2 files changed, 9 insertions(+), 8 deletions(-)

diff --git 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/ConnectionManager.java
 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/ConnectionManager.java
index 3695b60..f6c2a31 100644
--- 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/ConnectionManager.java
+++ 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/ConnectionManager.java
@@ -25,8 +25,6 @@ import com.zaxxer.hikari.HikariDataSource;
 import lombok.Getter;
 import 
org.apache.shardingsphere.driver.jdbc.adapter.executor.ForceExecuteTemplate;
 import 
org.apache.shardingsphere.driver.jdbc.adapter.invocation.MethodInvocationRecorder;
-import org.apache.shardingsphere.infra.database.metadata.DataSourceMetaData;
-import org.apache.shardingsphere.infra.database.type.DatabaseTypeRegistry;
 import 
org.apache.shardingsphere.infra.datasource.pool.creator.DataSourcePoolCreator;
 import org.apache.shardingsphere.infra.datasource.props.DataSourceProperties;
 import 
org.apache.shardingsphere.infra.executor.sql.execute.engine.ConnectionMode;
@@ -121,11 +119,10 @@ public final class ConnectionManager implements 
ExecutorJDBCManager, AutoCloseab
     
     private String createJdbcUrl(final ComputeNodeInstance instance, final 
String schema, final Map<String, Object> props) {
         String jdbcUrl = String.valueOf(props.get("jdbcUrl"));
-        String username = String.valueOf(props.get("username"));
-        DataSourceMetaData dataSourceMetaData = 
DatabaseTypeRegistry.getDatabaseTypeByURL(jdbcUrl).getDataSourceMetaData(jdbcUrl,
 username);
         InstanceId instanceId = 
instance.getInstanceDefinition().getInstanceId();
-        return jdbcUrl.replace(dataSourceMetaData.getHostname(), 
instanceId.getIp())
-                .replace(String.valueOf(dataSourceMetaData.getPort()), 
String.valueOf(instanceId.getUniqueSign())).replace(dataSourceMetaData.getCatalog(),
 schema);
+        String jdbcUrlPrefix = jdbcUrl.substring(0, jdbcUrl.indexOf("//"));
+        String jdbcUrlSuffix = jdbcUrl.contains("?") ? 
jdbcUrl.substring(jdbcUrl.indexOf("?")) : "";
+        return String.format("%s//%s:%s/%s%s", jdbcUrlPrefix, 
instanceId.getIp(), instanceId.getUniqueSign(), schema, jdbcUrlSuffix);
     }
     
     private ConnectionTransaction createConnectionTransaction(final String 
schemaName, final ContextManager contextManager) {
diff --git 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/ConnectionManagerTest.java
 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/ConnectionManagerTest.java
index e4a75f7..b833536 100644
--- 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/ConnectionManagerTest.java
+++ 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/ConnectionManagerTest.java
@@ -160,8 +160,10 @@ public final class ConnectionManagerTest {
     
     @Test
     public void assertGetConnectionWhenConfigTrafficRule() throws SQLException 
{
-        assertThat(connectionManager.getConnections("127.0.0.1@3307", 1, 
ConnectionMode.MEMORY_STRICTLY),
-                is(connectionManager.getConnections("127.0.0.1@3307", 1, 
ConnectionMode.MEMORY_STRICTLY)));
+        List<Connection> actual = 
connectionManager.getConnections("127.0.0.1@3307", 1, 
ConnectionMode.MEMORY_STRICTLY);
+        assertThat(actual, 
is(connectionManager.getConnections("127.0.0.1@3307", 1, 
ConnectionMode.MEMORY_STRICTLY)));
+        assertThat(actual.size(), is(1));
+        assertThat(actual.get(0).getMetaData().getURL(), 
is("jdbc:mock://127.0.0.1/foo_ds"));
     }
     
     @Test
@@ -178,6 +180,7 @@ public final class ConnectionManagerTest {
         List<Connection> actual = 
connectionManager.getConnections("127.0.0.1@3307", 1, 
ConnectionMode.CONNECTION_STRICTLY);
         assertThat(actual.size(), is(1));
         assertThat(actual.get(0), is(expected));
+        assertThat(actual.get(0).getMetaData().getURL(), 
is("jdbc:mock://127.0.0.1/foo_ds"));
     }
     
     @Test
@@ -190,6 +193,7 @@ public final class ConnectionManagerTest {
     public void assertGetConnectionsWhenConfigTrafficRuleAndEmptyCache() 
throws SQLException {
         List<Connection> actual = 
connectionManager.getConnections("127.0.0.1@3307", 1, 
ConnectionMode.MEMORY_STRICTLY);
         assertThat(actual.size(), is(1));
+        assertThat(actual.get(0).getMetaData().getURL(), 
is("jdbc:mock://127.0.0.1/foo_ds"));
     }
     
     @Test

Reply via email to