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

(shardingsphere) branch master updated: Refactor ShardingSphereDataSource (#31688)

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 329271ed012 Refactor ShardingSphereDataSource (#31688)
329271ed012 is described below

commit 329271ed01251626d932b70a4c08d2edeaf3ef7d
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Thu Jun 13 18:28:59 2024 +0800

    Refactor ShardingSphereDataSource (#31688)
    
    * Refactor ShardingSphereDataSource
    
    * Refactor ShardingSphereDataSource
---
 .../core/datasource/ShardingSphereDataSource.java  | 45 +++++++++-------------
 1 file changed, 19 insertions(+), 26 deletions(-)

diff --git a/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSource.java b/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSource.java
index b79c5b557f6..7d093fc7568 100644
--- a/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSource.java
+++ b/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSource.java
@@ -57,19 +57,17 @@ public final class ShardingSphereDataSource extends AbstractDataSourceAdapter im
     private final ContextManager contextManager;
     
     public ShardingSphereDataSource(final String databaseName, final ModeConfiguration modeConfig) throws SQLException {
-        this.databaseName = databaseName;
-        contextManager = createContextManager(databaseName, modeConfig, new LinkedHashMap<>(), new LinkedList<>(), new Properties());
-        contextManagerInitializedCallback(databaseName, contextManager);
+        this(databaseName, modeConfig, new LinkedHashMap<>(), new LinkedList<>(), new Properties());
     }
     
     public ShardingSphereDataSource(final String databaseName, final ModeConfiguration modeConfig, final Map<String, DataSource> dataSourceMap,
                                     final Collection<RuleConfiguration> ruleConfigs, final Properties props) throws SQLException {
         this.databaseName = databaseName;
-        contextManager = createContextManager(databaseName, modeConfig, dataSourceMap, ruleConfigs, null == props ? new Properties() : props);
-        contextManagerInitializedCallback(databaseName, contextManager);
+        contextManager = createContextManager(modeConfig, dataSourceMap, ruleConfigs, null == props ? new Properties() : props);
+        contextManagerInitializedCallback();
     }
     
-    private ContextManager createContextManager(final String databaseName, final ModeConfiguration modeConfig, final Map<String, DataSource> dataSourceMap,
+    private ContextManager createContextManager(final ModeConfiguration modeConfig, final Map<String, DataSource> dataSourceMap,
                                                 final Collection<RuleConfiguration> ruleConfigs, final Properties props) throws SQLException {
         InstanceMetaData instanceMetaData = TypedSPILoader.getService(InstanceMetaDataBuilder.class, "JDBC").build(-1);
         Collection<RuleConfiguration> globalRuleConfigs = ruleConfigs.stream().filter(GlobalRuleConfiguration.class::isInstance).collect(Collectors.toList());
@@ -80,7 +78,7 @@ public final class ShardingSphereDataSource extends AbstractDataSourceAdapter im
         return TypedSPILoader.getService(ContextManagerBuilder.class, null == modeConfig ? null : modeConfig.getType()).build(param, new EventBusContext());
     }
     
-    private void contextManagerInitializedCallback(final String databaseName, final ContextManager contextManager) {
+    private void contextManagerInitializedCallback() {
         for (ContextManagerLifecycleListener each : ShardingSphereServiceLoader.getServiceInstances(ContextManagerLifecycleListener.class)) {
             try {
                 each.onInitialized(databaseName, contextManager);
@@ -103,6 +101,19 @@ public final class ShardingSphereDataSource extends AbstractDataSourceAdapter im
         return getConnection();
     }
     
+    @Override
+    public int getLoginTimeout() throws SQLException {
+        Map<String, StorageUnit> storageUnits = contextManager.getStorageUnits(databaseName);
+        return storageUnits.isEmpty() ? 0 : storageUnits.values().iterator().next().getDataSource().getLoginTimeout();
+    }
+    
+    @Override
+    public void setLoginTimeout(final int seconds) throws SQLException {
+        for (StorageUnit each : contextManager.getStorageUnits(databaseName).values()) {
+            each.getDataSource().setLoginTimeout(seconds);
+        }
+    }
+    
     @Override
     public void close() throws SQLException {
         contextManagerDestroyedCallback(databaseName);
@@ -126,25 +137,7 @@ public final class ShardingSphereDataSource extends AbstractDataSourceAdapter im
     
     private void contextManagerDestroyedCallback(final String databaseName) {
         for (ContextManagerLifecycleListener each : ShardingSphereServiceLoader.getServiceInstances(ContextManagerLifecycleListener.class)) {
-            try {
-                each.onDestroyed(databaseName, InstanceType.JDBC);
-                // CHECKSTYLE:OFF
-            } catch (final RuntimeException ignored) {
-                // CHECKSTYLE:ON
-            }
-        }
-    }
-    
-    @Override
-    public int getLoginTimeout() throws SQLException {
-        Map<String, StorageUnit> storageUnits = contextManager.getStorageUnits(databaseName);
-        return storageUnits.isEmpty() ? 0 : storageUnits.values().iterator().next().getDataSource().getLoginTimeout();
-    }
-    
-    @Override
-    public void setLoginTimeout(final int seconds) throws SQLException {
-        for (StorageUnit each : contextManager.getStorageUnits(databaseName).values()) {
-            each.getDataSource().setLoginTimeout(seconds);
+            each.onDestroyed(databaseName, InstanceType.JDBC);
         }
     }
 }