From faf3f245f62cd54df31083dd152cbdd78c2b829a Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Mon, 17 Jun 2024 23:48:12 +0800 Subject: [PATCH] Refactor StorageUnit to use CatalogSwitchableDataSource only necessary (#31737) --- .../infra/metadata/database/resource/unit/StorageUnit.java | 4 ++-- .../impl/DataSourceGeneratedDatabaseConfigurationTest.java | 7 ++----- .../impl/DataSourceProvidedDatabaseConfigurationTest.java | 7 ++----- 3 files changed, 6 insertions(+), 12 deletions(-) diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnit.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnit.java index 5724cbe8c125a..8f488667a640a 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnit.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnit.java @@ -57,8 +57,8 @@ public StorageUnit(final StorageNode storageNode, final DataSourcePoolProperties storageType = DatabaseTypeFactory.get(url); boolean isInstanceConnectionAvailable = new DatabaseTypeRegistry(DatabaseTypeFactory.get(url)).getDialectDatabaseMetaData().isInstanceConnectionAvailable(); catalog = isInstanceConnectionAvailable ? new StandardJdbcUrlParser().parse(url).getDatabase() : null; - this.dataSource = new CatalogSwitchableDataSource(dataSource, catalog, url); - this.dataSourcePoolProperties = dataSourcePoolProps; + this.dataSource = isInstanceConnectionAvailable ? new CatalogSwitchableDataSource(dataSource, catalog, url) : dataSource; + dataSourcePoolProperties = dataSourcePoolProps; connectionProperties = createConnectionProperties(standardProps); } diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceGeneratedDatabaseConfigurationTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceGeneratedDatabaseConfigurationTest.java index db7ab6ae02a7b..2048ec52ab68d 100644 --- a/infra/common/src/test/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceGeneratedDatabaseConfigurationTest.java +++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceGeneratedDatabaseConfigurationTest.java @@ -18,7 +18,6 @@ package org.apache.shardingsphere.infra.config.database.impl; import com.zaxxer.hikari.HikariDataSource; -import org.apache.shardingsphere.infra.datasource.pool.CatalogSwitchableDataSource; import org.apache.shardingsphere.infra.datasource.pool.config.ConnectionConfiguration; import org.apache.shardingsphere.infra.datasource.pool.config.DataSourceConfiguration; import org.apache.shardingsphere.infra.datasource.pool.config.PoolConfiguration; @@ -43,8 +42,7 @@ class DataSourceGeneratedDatabaseConfigurationTest { void assertGetDataSources() { DataSourceGeneratedDatabaseConfiguration databaseConfig = createDataSourceGeneratedDatabaseConfiguration(); DataSource dataSource = databaseConfig.getStorageUnits().get("normal_db").getDataSource(); - assertTrue(dataSource instanceof CatalogSwitchableDataSource); - assertTrue(((CatalogSwitchableDataSource) dataSource).getDataSource() instanceof HikariDataSource); + assertTrue(dataSource instanceof HikariDataSource); } @Test @@ -60,8 +58,7 @@ void assertGetStorageNodes() { void assertGetStorageUnits() { DataSourceGeneratedDatabaseConfiguration databaseConfig = createDataSourceGeneratedDatabaseConfiguration(); DataSource dataSource = databaseConfig.getStorageUnits().get("normal_db").getDataSource(); - assertTrue(dataSource instanceof CatalogSwitchableDataSource); - assertTrue(((CatalogSwitchableDataSource) dataSource).getDataSource() instanceof HikariDataSource); + assertTrue(dataSource instanceof HikariDataSource); } @Test diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceProvidedDatabaseConfigurationTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceProvidedDatabaseConfigurationTest.java index 64287f762af5b..6c39e9086389b 100644 --- a/infra/common/src/test/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceProvidedDatabaseConfigurationTest.java +++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceProvidedDatabaseConfigurationTest.java @@ -17,7 +17,6 @@ package org.apache.shardingsphere.infra.config.database.impl; -import org.apache.shardingsphere.infra.datasource.pool.CatalogSwitchableDataSource; import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; import org.apache.shardingsphere.infra.fixture.FixtureRuleConfiguration; import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode; @@ -38,8 +37,7 @@ class DataSourceProvidedDatabaseConfigurationTest { void assertGetDataSources() { DataSourceProvidedDatabaseConfiguration databaseConfig = createDataSourceProvidedDatabaseConfiguration(); DataSource dataSource = databaseConfig.getStorageUnits().get("foo_ds").getDataSource(); - assertTrue(dataSource instanceof CatalogSwitchableDataSource); - assertTrue(((CatalogSwitchableDataSource) dataSource).getDataSource() instanceof MockedDataSource); + assertTrue(dataSource instanceof MockedDataSource); } @Test @@ -55,8 +53,7 @@ void assertGetStorageNodes() { void assertGetStorageUnits() { DataSourceProvidedDatabaseConfiguration databaseConfig = createDataSourceProvidedDatabaseConfiguration(); DataSource dataSource = databaseConfig.getStorageUnits().get("foo_ds").getDataSource(); - assertTrue(dataSource instanceof CatalogSwitchableDataSource); - assertTrue(((CatalogSwitchableDataSource) dataSource).getDataSource() instanceof MockedDataSource); + assertTrue(dataSource instanceof MockedDataSource); } @Test