diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSource.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSource.java index b3177ef5782f12a284bbbe490db708756c89970c..a95ef9a623502b4ea767f6db20155cddd9214e69 100644 --- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSource.java +++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSource.java @@ -19,6 +19,7 @@ package org.apache.shardingsphere.driver.jdbc.core.datasource; import com.google.common.base.Preconditions; import lombok.Getter; +import lombok.RequiredArgsConstructor; import lombok.Setter; import org.apache.shardingsphere.driver.jdbc.core.connection.ShardingSphereConnection; import org.apache.shardingsphere.driver.jdbc.unsupported.AbstractUnsupportedOperationDataSource; @@ -44,6 +45,7 @@ import java.util.logging.Logger; /** * ShardingSphere data source. */ +@RequiredArgsConstructor @Getter public final class ShardingSphereDataSource extends AbstractUnsupportedOperationDataSource implements AutoCloseable { diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-orchestration/src/main/java/org/apache/shardingsphere/driver/orchestration/internal/datasource/OrchestrationShardingSphereDataSource.java b/shardingsphere-jdbc/shardingsphere-jdbc-orchestration/src/main/java/org/apache/shardingsphere/driver/orchestration/internal/datasource/OrchestrationShardingSphereDataSource.java index 6803b1e9325118ed1aa6e97ada8152466a08a1ba..343f4b3bfe724283e2fb1f893c8491cf11574353 100644 --- a/shardingsphere-jdbc/shardingsphere-jdbc-orchestration/src/main/java/org/apache/shardingsphere/driver/orchestration/internal/datasource/OrchestrationShardingSphereDataSource.java +++ b/shardingsphere-jdbc/shardingsphere-jdbc-orchestration/src/main/java/org/apache/shardingsphere/driver/orchestration/internal/datasource/OrchestrationShardingSphereDataSource.java @@ -28,9 +28,13 @@ import org.apache.shardingsphere.infra.config.DataSourceConfiguration; import org.apache.shardingsphere.infra.config.RuleConfiguration; import org.apache.shardingsphere.infra.database.DefaultSchema; import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; +import org.apache.shardingsphere.infra.metadata.schema.RuleSchemaMetaData; import org.apache.shardingsphere.infra.rule.ShardingSphereRule; import org.apache.shardingsphere.infra.rule.StatusContainedRule; import org.apache.shardingsphere.infra.rule.event.impl.DataSourceNameDisabledEvent; +import org.apache.shardingsphere.kernal.context.SchemaContext; +import org.apache.shardingsphere.kernal.context.SchemaContexts; +import org.apache.shardingsphere.kernal.context.schema.ShardingSphereSchema; import org.apache.shardingsphere.orchestration.center.config.OrchestrationConfiguration; import org.apache.shardingsphere.orchestration.core.common.event.DataSourceChangedEvent; import org.apache.shardingsphere.orchestration.core.common.event.PropertiesChangedEvent; @@ -44,6 +48,7 @@ import org.apache.shardingsphere.orchestration.core.registrycenter.schema.Orches import java.sql.SQLException; import java.util.Collection; import java.util.Collections; +import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; @@ -87,12 +92,14 @@ public class OrchestrationShardingSphereDataSource extends AbstractOrchestration */ @Subscribe public final synchronized void renew(final MetaDataChangedEvent event) { - for (String each : event.getSchemaNames()) { - if (DefaultSchema.LOGIC_NAME.equals(each)) { - dataSource.getSchemaContexts().getDefaultSchemaContext().getSchema().setMetaData( - new ShardingSphereMetaData(dataSource.getSchemaContexts().getDefaultSchemaContext().getSchema().getMetaData().getDataSources(), event.getRuleSchemaMetaData())); - } + if (!event.getSchemaNames().contains(DefaultSchema.LOGIC_NAME)) { + return; } + Map schemaContexts = new HashMap<>(dataSource.getSchemaContexts().getSchemaContexts().size()); + SchemaContext oldSchemaContext = dataSource.getSchemaContexts().getSchemaContexts().get(DefaultSchema.LOGIC_NAME); + schemaContexts.put(DefaultSchema.LOGIC_NAME, new SchemaContext(oldSchemaContext.getName(), + getChangedShardingSphereSchema(oldSchemaContext.getSchema(), event.getRuleSchemaMetaData()), oldSchemaContext.getRuntimeContext())); + dataSource = new ShardingSphereDataSource(new SchemaContexts(schemaContexts, dataSource.getSchemaContexts().getProperties(), dataSource.getSchemaContexts().getAuthentication())); } /** @@ -152,4 +159,10 @@ public class OrchestrationShardingSphereDataSource extends AbstractOrchestration } } } + + private ShardingSphereSchema getChangedShardingSphereSchema(final ShardingSphereSchema oldShardingSphereSchema, final RuleSchemaMetaData newRuleSchemaMetaData) { + ShardingSphereMetaData metaData = new ShardingSphereMetaData(oldShardingSphereSchema.getMetaData().getDataSources(), newRuleSchemaMetaData); + return new ShardingSphereSchema(oldShardingSphereSchema.getDatabaseType(), oldShardingSphereSchema.getConfigurations(), + oldShardingSphereSchema.getRules(), oldShardingSphereSchema.getDataSources(), metaData); + } } diff --git a/shardingsphere-kernal/shardingsphere-kernal-context/src/main/java/org/apache/shardingsphere/kernal/context/SchemaContext.java b/shardingsphere-kernal/shardingsphere-kernal-context/src/main/java/org/apache/shardingsphere/kernal/context/SchemaContext.java index 3e4887ebb3b8cf6565cc5ec728c3d7fd62e4ba1f..c35840abf15b168f6f22d2e71b0ed84e8849b694 100644 --- a/shardingsphere-kernal/shardingsphere-kernal-context/src/main/java/org/apache/shardingsphere/kernal/context/SchemaContext.java +++ b/shardingsphere-kernal/shardingsphere-kernal-context/src/main/java/org/apache/shardingsphere/kernal/context/SchemaContext.java @@ -19,12 +19,8 @@ package org.apache.shardingsphere.kernal.context; import lombok.Getter; import org.apache.shardingsphere.kernal.context.runtime.RuntimeContext; -import org.apache.shardingsphere.kernal.context.schema.DataSourceParameter; import org.apache.shardingsphere.kernal.context.schema.ShardingSphereSchema; -import javax.sql.DataSource; -import java.util.Map; - @Getter public final class SchemaContext { @@ -39,20 +35,4 @@ public final class SchemaContext { this.schema = schema; this.runtimeContext = runtimeContext; } - - /** - * Renew data sources. - * - * @param dataSourceParameters data source parameters - * @param dataSources data sources - * @throws Exception exception - */ - public void renew(final Map dataSourceParameters, final Map dataSources) throws Exception { - schema.getDataSources().clear(); - schema.getDataSources().putAll(dataSources); - schema.getDataSourceParameters().clear(); - schema.getDataSourceParameters().putAll(dataSourceParameters); - runtimeContext.getTransactionManagerEngine().close(); - runtimeContext.getTransactionManagerEngine().init(schema.getDatabaseType(), schema.getDataSources()); - } } diff --git a/shardingsphere-kernal/shardingsphere-kernal-context/src/main/java/org/apache/shardingsphere/kernal/context/SchemaContexts.java b/shardingsphere-kernal/shardingsphere-kernal-context/src/main/java/org/apache/shardingsphere/kernal/context/SchemaContexts.java index 206c25b711cb67291c2e857a3d621367a6749454..e2e57ed10700b13f8dd4f171dcf7acd204d03bfb 100644 --- a/shardingsphere-kernal/shardingsphere-kernal-context/src/main/java/org/apache/shardingsphere/kernal/context/SchemaContexts.java +++ b/shardingsphere-kernal/shardingsphere-kernal-context/src/main/java/org/apache/shardingsphere/kernal/context/SchemaContexts.java @@ -18,12 +18,9 @@ package org.apache.shardingsphere.kernal.context; import lombok.Getter; -import lombok.Setter; import org.apache.shardingsphere.infra.auth.Authentication; import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties; -import org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey; import org.apache.shardingsphere.infra.database.DefaultSchema; -import org.apache.shardingsphere.infra.executor.kernel.ExecutorKernel; import java.util.HashMap; import java.util.Map; @@ -34,10 +31,9 @@ public final class SchemaContexts { private final Map schemaContexts = new HashMap<>(); - private ConfigurationProperties properties; + private final ConfigurationProperties properties; - @Setter - private Authentication authentication; + private final Authentication authentication; public SchemaContexts() { properties = new ConfigurationProperties(new Properties()); @@ -59,16 +55,6 @@ public final class SchemaContexts { return schemaContexts.get(DefaultSchema.LOGIC_NAME); } - /** - * Renew. - * - * @param properties properties - */ - public void renew(final ConfigurationProperties properties) { - this.properties = properties; - new ExecutorKernel(properties.getValue(ConfigurationPropertyKey.EXECUTOR_SIZE)); - } - /** * Close. * diff --git a/shardingsphere-kernal/shardingsphere-kernal-context/src/main/java/org/apache/shardingsphere/kernal/context/schema/ShardingSphereSchema.java b/shardingsphere-kernal/shardingsphere-kernal-context/src/main/java/org/apache/shardingsphere/kernal/context/schema/ShardingSphereSchema.java index c248ad8b0373fe71504ff970270ca7d0d4649583..aa32a87278d3464806958dadb84391f9bd16aa1b 100644 --- a/shardingsphere-kernal/shardingsphere-kernal-context/src/main/java/org/apache/shardingsphere/kernal/context/schema/ShardingSphereSchema.java +++ b/shardingsphere-kernal/shardingsphere-kernal-context/src/main/java/org/apache/shardingsphere/kernal/context/schema/ShardingSphereSchema.java @@ -18,12 +18,10 @@ package org.apache.shardingsphere.kernal.context.schema; import lombok.Getter; -import lombok.Setter; import org.apache.shardingsphere.infra.config.RuleConfiguration; import org.apache.shardingsphere.infra.database.type.DatabaseType; import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; import org.apache.shardingsphere.infra.rule.ShardingSphereRule; -import org.apache.shardingsphere.infra.rule.ShardingSphereRulesBuilder; import javax.sql.DataSource; import java.lang.reflect.Method; @@ -45,8 +43,7 @@ public final class ShardingSphereSchema { private final Map dataSourceParameters = new LinkedHashMap<>(); - @Setter - private ShardingSphereMetaData metaData; + private final ShardingSphereMetaData metaData; public ShardingSphereSchema(final DatabaseType databaseType, final Collection configurations, final Collection rules, final Map dataSourceMap, final ShardingSphereMetaData shardingSphereMetaData) { @@ -63,19 +60,6 @@ public final class ShardingSphereSchema { this.dataSourceParameters.putAll(dataSourceParameters); } - /** - * Set configurations. - * - * @param configurations rule configurations - */ - public void renew(final Collection configurations) { - this.configurations.clear(); - this.configurations.addAll(configurations); - Collection rules = ShardingSphereRulesBuilder.build(configurations, dataSources.keySet()); - this.rules.clear(); - this.rules.addAll(rules); - } - /** * Close data sources. * @param dataSources data sources