You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2022/04/30 14:57:00 UTC

[shardingsphere] branch master updated: Add DriverStateFactory (#17237)

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

panjuan 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 bc0f9004b05 Add DriverStateFactory (#17237)
bc0f9004b05 is described below

commit bc0f9004b050a6cbe93ca740f4284a7fb3656a3c
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sat Apr 30 22:56:47 2022 +0800

    Add DriverStateFactory (#17237)
---
 .../driver/state/DriverStateContext.java           |  9 +--------
 ...erStateContext.java => DriverStateFactory.java} | 22 +++++++++-------------
 2 files changed, 10 insertions(+), 21 deletions(-)

diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/state/DriverStateContext.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/state/DriverStateContext.java
index db80aa6c7a1..0fdb4102916 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/state/DriverStateContext.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/state/DriverStateContext.java
@@ -20,8 +20,6 @@ package org.apache.shardingsphere.driver.state;
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
 import org.apache.shardingsphere.mode.manager.ContextManager;
-import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
-import org.apache.shardingsphere.spi.type.typed.TypedSPIRegistry;
 
 import java.sql.Connection;
 
@@ -31,10 +29,6 @@ import java.sql.Connection;
 @NoArgsConstructor(access = AccessLevel.PRIVATE)
 public final class DriverStateContext {
     
-    static {
-        ShardingSphereServiceLoader.register(DriverState.class);
-    }
-    
     /**
      * Get connection.
      *
@@ -43,7 +37,6 @@ public final class DriverStateContext {
      * @return connection
      */
     public static Connection getConnection(final String schemaName, final ContextManager contextManager) {
-        return TypedSPIRegistry.getRegisteredService(DriverState.class,
-                contextManager.getInstanceContext().getInstance().getState().getCurrentState().name()).getConnection(schemaName, contextManager);
+        return DriverStateFactory.newInstance(contextManager.getInstanceContext().getInstance().getState().getCurrentState()).getConnection(schemaName, contextManager);
     }
 }
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/state/DriverStateContext.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/state/DriverStateFactory.java
similarity index 69%
copy from shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/state/DriverStateContext.java
copy to shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/state/DriverStateFactory.java
index db80aa6c7a1..f4a95a6cf5a 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/state/DriverStateContext.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/state/DriverStateFactory.java
@@ -19,31 +19,27 @@ package org.apache.shardingsphere.driver.state;
 
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
-import org.apache.shardingsphere.mode.manager.ContextManager;
+import org.apache.shardingsphere.infra.state.StateType;
 import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
 import org.apache.shardingsphere.spi.type.typed.TypedSPIRegistry;
 
-import java.sql.Connection;
-
 /**
- * Driver state context.
+ * Driver state factory.
  */
 @NoArgsConstructor(access = AccessLevel.PRIVATE)
-public final class DriverStateContext {
+public final class DriverStateFactory {
     
     static {
         ShardingSphereServiceLoader.register(DriverState.class);
     }
     
     /**
-     * Get connection.
-     *
-     * @param schemaName schema name
-     * @param contextManager context manager
-     * @return connection
+     * Create new instance of driver state.
+     * 
+     * @param type state type 
+     * @return new instance of driver state
      */
-    public static Connection getConnection(final String schemaName, final ContextManager contextManager) {
-        return TypedSPIRegistry.getRegisteredService(DriverState.class,
-                contextManager.getInstanceContext().getInstance().getState().getCurrentState().name()).getConnection(schemaName, contextManager);
+    public static DriverState newInstance(final StateType type) {
+        return TypedSPIRegistry.getRegisteredService(DriverState.class, type.name());
     }
 }