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 6fa502a13bf Fix #19224 (#19445)
6fa502a13bf is described below
commit 6fa502a13bfe9a4bd2769f742c40677b6f3bef85
Author: JingShang Lu <[email protected]>
AuthorDate: Mon Jul 25 13:44:41 2022 +0800
Fix #19224 (#19445)
* Add protocol mismatch check for datasource
* fix
* Update DataSourcePropertiesValidator.java
---
.../datasource/props/DataSourcePropertiesValidator.java | 16 ++++++++++------
.../props/DataSourcePropertiesValidatorTest.java | 11 +++++++++--
.../updatable/ImportDatabaseConfigurationHandler.java | 2 +-
.../distsql/rdl/resource/AddResourceBackendHandler.java | 2 +-
.../rdl/resource/AlterResourceBackendHandler.java | 2 +-
5 files changed, 22 insertions(+), 11 deletions(-)
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/props/DataSourcePropertiesValidator.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/props/DataSourcePropertiesValidator.java
index 4a1de267b6d..3f457f4f970 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/props/DataSourcePropertiesValidator.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/props/DataSourcePropertiesValidator.java
@@ -17,6 +17,7 @@
package org.apache.shardingsphere.infra.datasource.props;
+import org.apache.shardingsphere.infra.database.type.DatabaseType;
import
org.apache.shardingsphere.infra.datasource.pool.creator.DataSourcePoolCreator;
import
org.apache.shardingsphere.infra.datasource.pool.destroyer.DataSourcePoolDestroyer;
import
org.apache.shardingsphere.infra.distsql.exception.resource.InvalidResourcesException;
@@ -37,13 +38,14 @@ public final class DataSourcePropertiesValidator {
* Validate data source properties map.
*
* @param dataSourcePropertiesMap data source properties map
+ * @param databaseType database type
* @throws InvalidResourcesException invalid resources exception
*/
- public void validate(final Map<String, DataSourceProperties>
dataSourcePropertiesMap) throws InvalidResourcesException {
+ public void validate(final Map<String, DataSourceProperties>
dataSourcePropertiesMap, final DatabaseType databaseType) throws
InvalidResourcesException {
Collection<String> errorMessages = new LinkedList<>();
for (Entry<String, DataSourceProperties> entry :
dataSourcePropertiesMap.entrySet()) {
try {
- validate(entry.getKey(), entry.getValue());
+ validate(entry.getKey(), entry.getValue(), databaseType);
} catch (final InvalidDataSourcePropertiesException ex) {
errorMessages.add(ex.getMessage());
}
@@ -53,11 +55,11 @@ public final class DataSourcePropertiesValidator {
}
}
- private void validate(final String dataSourceName, final
DataSourceProperties dataSourceProps) throws
InvalidDataSourcePropertiesException {
+ private void validate(final String dataSourceName, final
DataSourceProperties dataSourceProps, final DatabaseType databaseType) throws
InvalidDataSourcePropertiesException {
DataSource dataSource = null;
try {
dataSource = DataSourcePoolCreator.create(dataSourceProps);
- checkFailFast(dataSource);
+ checkFailFast(dataSource, databaseType);
// CHECKSTYLE:OFF
} catch (final Exception ex) {
// CHECKSTYLE:ON
@@ -69,7 +71,9 @@ public final class DataSourcePropertiesValidator {
}
}
- private void checkFailFast(final DataSource dataSource) throws
SQLException {
- dataSource.getConnection();
+ private void checkFailFast(final DataSource dataSource, final DatabaseType
databaseType) throws SQLException {
+ if
(!dataSource.getConnection().getMetaData().getDatabaseProductName().equals(databaseType.getType()))
{
+ throw new SQLException("Protocol mismatch for data source.");
+ }
}
}
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datasource/props/DataSourcePropertiesValidatorTest.java
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datasource/props/DataSourcePropertiesValidatorTest.java
index 9dcecd82e39..7362d9194ee 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datasource/props/DataSourcePropertiesValidatorTest.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datasource/props/DataSourcePropertiesValidatorTest.java
@@ -18,6 +18,8 @@
package org.apache.shardingsphere.infra.datasource.props;
import com.zaxxer.hikari.HikariDataSource;
+import org.apache.shardingsphere.infra.database.type.dialect.H2DatabaseType;
+import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
import
org.apache.shardingsphere.infra.distsql.exception.resource.InvalidResourcesException;
import org.junit.Test;
@@ -29,7 +31,12 @@ public final class DataSourcePropertiesValidatorTest {
@Test
public void assertValidateSuccess() throws InvalidResourcesException {
- new
DataSourcePropertiesValidator().validate(Collections.singletonMap("name", new
DataSourceProperties(HikariDataSource.class.getName(),
createValidProperties())));
+ new
DataSourcePropertiesValidator().validate(Collections.singletonMap("name", new
DataSourceProperties(HikariDataSource.class.getName(),
createValidProperties())), new H2DatabaseType());
+ }
+
+ @Test(expected = InvalidResourcesException.class)
+ public void assertDatabaseTypeInValidateFail() throws
InvalidResourcesException {
+ new
DataSourcePropertiesValidator().validate(Collections.singletonMap("name", new
DataSourceProperties(HikariDataSource.class.getName(),
createValidProperties())), new MySQLDatabaseType());
}
private Map<String, Object> createValidProperties() {
@@ -43,7 +50,7 @@ public final class DataSourcePropertiesValidatorTest {
@Test(expected = InvalidResourcesException.class)
public void assertValidateFailed() throws InvalidResourcesException {
- new
DataSourcePropertiesValidator().validate(Collections.singletonMap("name", new
DataSourceProperties(HikariDataSource.class.getName(),
createInvalidProperties())));
+ new
DataSourcePropertiesValidator().validate(Collections.singletonMap("name", new
DataSourceProperties(HikariDataSource.class.getName(),
createInvalidProperties())), new H2DatabaseType());
}
private Map<String, Object> createInvalidProperties() {
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/updatable/ImportDatabaseConfigurationHandler.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/updatable/ImportDatabaseConfigurationHandler.java
index 5c45dc49e03..ea304c3341e 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/updatable/ImportDatabaseConfigurationHandler.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/updatable/ImportDatabaseConfigurationHandler.java
@@ -131,7 +131,7 @@ public final class ImportDatabaseConfigurationHandler
extends UpdatableRALBacken
for (Entry<String, YamlProxyDataSourceConfiguration> entry :
yamlDataSourceMap.entrySet()) {
dataSourcePropsMap.put(entry.getKey(),
DataSourcePropertiesCreator.create(HikariDataSource.class.getName(),
dataSourceConfigSwapper.swap(entry.getValue())));
}
- validator.validate(dataSourcePropsMap);
+ validator.validate(dataSourcePropsMap,
getConnectionSession().getDatabaseType());
try {
ProxyContext.getInstance().getContextManager().updateResources(databaseName,
dataSourcePropsMap);
} catch (final SQLException ex) {
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/AddResourceBackendHandler.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/AddResourceBackendHandler.java
index 73e598a1325..07f687b27b2 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/AddResourceBackendHandler.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/AddResourceBackendHandler.java
@@ -60,7 +60,7 @@ public final class AddResourceBackendHandler extends
DatabaseRequiredBackendHand
public ResponseHeader execute(final String databaseName, final
AddResourceStatement sqlStatement) throws DistSQLException {
checkSQLStatement(databaseName, sqlStatement);
Map<String, DataSourceProperties> dataSourcePropsMap =
ResourceSegmentsConverter.convert(databaseType, sqlStatement.getDataSources());
- validator.validate(dataSourcePropsMap);
+ validator.validate(dataSourcePropsMap, databaseType);
try {
ProxyContext.getInstance().getContextManager().updateResources(databaseName,
dataSourcePropsMap);
} catch (final SQLException | ShardingSphereException ex) {
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/AlterResourceBackendHandler.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/AlterResourceBackendHandler.java
index 4ccc8869764..69fd858d092 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/AlterResourceBackendHandler.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/AlterResourceBackendHandler.java
@@ -67,7 +67,7 @@ public final class AlterResourceBackendHandler extends
DatabaseRequiredBackendHa
public ResponseHeader execute(final String databaseName, final
AlterResourceStatement sqlStatement) throws DistSQLException {
checkSQLStatement(databaseName, sqlStatement);
Map<String, DataSourceProperties> dataSourcePropsMap =
ResourceSegmentsConverter.convert(databaseType, sqlStatement.getDataSources());
- validator.validate(dataSourcePropsMap);
+ validator.validate(dataSourcePropsMap, databaseType);
try {
ProxyContext.getInstance().getContextManager().updateResources(databaseName,
dataSourcePropsMap);
} catch (final SQLException | ShardingSphereException ex) {