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 2020/09/07 09:21:09 UTC
[shardingsphere] branch master updated: Fix issue for
HikariJDBCParameterDecorator can not append JDBC parameters (#7290)
This is an automated email from the ASF dual-hosted git repository.
zhangyonglun 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 4ea7510 Fix issue for HikariJDBCParameterDecorator can not append JDBC parameters (#7290)
4ea7510 is described below
commit 4ea75102192e9814507c6807807930d805a26364
Author: Liang Zhang <te...@163.com>
AuthorDate: Mon Sep 7 17:20:47 2020 +0800
Fix issue for HikariJDBCParameterDecorator can not append JDBC parameters (#7290)
* Remove useless fixture
* Refactor BootstrapArguments
* Refactor BootstrapArgumentsTest
* Fix issue for HikariJDBCParameterDecorator can not append JDBC parameters
---
.../schema/GovernanceSchemaContextsTest.java | 2 +-
.../config/datasource/DataSourceConfiguration.java | 6 +-
.../config/datasource/JDBCParameterDecorator.java | 3 +-
.../infra/context/SchemaContextsBuilderTest.java | 2 +-
.../spring/boot/SpringBootJNDIDataSourceTest.java | 2 +-
.../test/resources/application-common.properties | 4 +-
.../META-INF/spring/application-context.xml | 12 ++--
.../shardingsphere-proxy-backend/pom.xml | 7 +++
.../decorator/HikariJDBCParameterDecorator.java | 3 +-
.../factory/JDBCRawBackendDataSourceFactory.java | 6 +-
.../HikariJDBCParameterDecoratorTest.java | 8 ++-
.../src/test/resources/logback-test.xml | 33 +++++++++++
.../{ => jdbc}/test/MockedDataSource.java | 2 +-
.../shardingsphere/jdbc/test/MockedDriver.java | 68 ++++++++++++++++++++++
14 files changed, 135 insertions(+), 23 deletions(-)
diff --git a/shardingsphere-governance/shardingsphere-governance-core/shardingsphere-governance-core-context/src/test/java/org/apache/shardingsphere/governance/context/schema/GovernanceSchemaContextsTest.java b/shardingsphere-governance/shardingsphere-governance-core/shardingsphere-governance-core-context/src/test/java/org/apache/shardingsphere/governance/context/schema/GovernanceSchemaContextsTest.java
index 8bcc29e..7511493 100644
--- a/shardingsphere-governance/shardingsphere-governance-core/shardingsphere-governance-core-context/src/test/java/org/apache/shardingsphere/governance/context/schema/GovernanceSchemaContextsTest.java
+++ b/shardingsphere-governance/shardingsphere-governance-core/shardingsphere-governance-core-context/src/test/java/org/apache/shardingsphere/governance/context/schema/GovernanceSchemaContextsTest.java
@@ -44,7 +44,7 @@ import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.schema.RuleSchemaMetaData;
import org.apache.shardingsphere.infra.rule.event.RuleChangedEvent;
import org.apache.shardingsphere.masterslave.rule.MasterSlaveRule;
-import org.apache.shardingsphere.test.MockedDataSource;
+import org.apache.shardingsphere.jdbc.test.MockedDataSource;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/DataSourceConfiguration.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/DataSourceConfiguration.java
index ede6e3d..de197ef 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/DataSourceConfiguration.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/DataSourceConfiguration.java
@@ -102,7 +102,7 @@ public final class DataSourceConfiguration {
*
* @return data source
*/
- @SuppressWarnings("unchecked")
+ @SuppressWarnings({"unchecked", "rawtypes"})
@SneakyThrows(ReflectiveOperationException.class)
public DataSource createDataSource() {
DataSource result = (DataSource) Class.forName(dataSourceClassName).getConstructor().newInstance();
@@ -116,8 +116,8 @@ public final class DataSourceConfiguration {
setterMethod.get().invoke(result, entry.getValue());
}
}
- findJDBCParameterDecorator(result).ifPresent(decorator -> decorator.decorate(result));
- return result;
+ Optional<JDBCParameterDecorator> decorator = findJDBCParameterDecorator(result);
+ return decorator.isPresent() ? decorator.get().decorate(result) : result;
}
@SuppressWarnings("rawtypes")
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/JDBCParameterDecorator.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/JDBCParameterDecorator.java
index f6f26a8..057fc85 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/JDBCParameterDecorator.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/JDBCParameterDecorator.java
@@ -30,8 +30,9 @@ public interface JDBCParameterDecorator<T extends DataSource> {
* Decorate data source.
*
* @param dataSource data source to be decorated
+ * @return decorated data source
*/
- void decorate(T dataSource);
+ T decorate(T dataSource);
/**
* Get data source type.
diff --git a/shardingsphere-infra/shardingsphere-infra-context/src/test/java/org/apache/shardingsphere/infra/context/SchemaContextsBuilderTest.java b/shardingsphere-infra/shardingsphere-infra-context/src/test/java/org/apache/shardingsphere/infra/context/SchemaContextsBuilderTest.java
index 588680e..1bb8cdb 100644
--- a/shardingsphere-infra/shardingsphere-infra-context/src/test/java/org/apache/shardingsphere/infra/context/SchemaContextsBuilderTest.java
+++ b/shardingsphere-infra/shardingsphere-infra-context/src/test/java/org/apache/shardingsphere/infra/context/SchemaContextsBuilderTest.java
@@ -22,7 +22,7 @@ import org.apache.shardingsphere.infra.context.fixture.FixtureRule;
import org.apache.shardingsphere.infra.context.fixture.FixtureRuleConfiguration;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.database.type.DatabaseTypes;
-import org.apache.shardingsphere.test.MockedDataSource;
+import org.apache.shardingsphere.jdbc.test.MockedDataSource;
import org.hamcrest.CoreMatchers;
import org.junit.Test;
import org.junit.runner.RunWith;
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter/src/test/java/org/apache/shardingsphere/spring/boot/SpringBootJNDIDataSourceTest.java b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter/src/test/java/org/apache/shardingsphere/spring/boot/SpringBootJNDIDataSourceTest.java
index 11e3356..a8ef51b 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter/src/test/java/org/apache/shardingsphere/spring/boot/SpringBootJNDIDataSourceTest.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter/src/test/java/org/apache/shardingsphere/spring/boot/SpringBootJNDIDataSourceTest.java
@@ -19,7 +19,7 @@ package org.apache.shardingsphere.spring.boot;
import org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource;
import org.apache.shardingsphere.spring.boot.fixture.TestJndiInitialContextFactory;
-import org.apache.shardingsphere.test.MockedDataSource;
+import org.apache.shardingsphere.jdbc.test.MockedDataSource;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter/src/test/resources/application-common.properties b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter/src/test/resources/application-common.properties
index 94e26f9..9a0027e 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter/src/test/resources/application-common.properties
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter/src/test/resources/application-common.properties
@@ -17,8 +17,8 @@
spring.shardingsphere.datasource.names=ds_${0..1}
-spring.shardingsphere.datasource.ds_0.type=org.apache.shardingsphere.test.MockedDataSource
-spring.shardingsphere.datasource.ds_1.type=org.apache.shardingsphere.test.MockedDataSource
+spring.shardingsphere.datasource.ds_0.type=org.apache.shardingsphere.jdbc.test.MockedDataSource
+spring.shardingsphere.datasource.ds_1.type=org.apache.shardingsphere.jdbc.test.MockedDataSource
spring.shardingsphere.rules.master-slave.load-balancers.random.type=RANDOM
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-namespace/src/test/resources/META-INF/spring/application-context.xml b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-namespace/src/test/resources/META-INF/spring/application-context.xml
index 6587c49..f1b2ad4 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-namespace/src/test/resources/META-INF/spring/application-context.xml
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-namespace/src/test/resources/META-INF/spring/application-context.xml
@@ -38,12 +38,12 @@
">
<context:property-placeholder location="classpath:conf/conf.properties" />
- <bean id="ds_0_master" class="org.apache.shardingsphere.test.MockedDataSource" />
- <bean id="ds_0_slave_0" class="org.apache.shardingsphere.test.MockedDataSource" />
- <bean id="ds_0_slave_1" class="org.apache.shardingsphere.test.MockedDataSource" />
- <bean id="ds_1_master" class="org.apache.shardingsphere.test.MockedDataSource" />
- <bean id="ds_1_slave_0" class="org.apache.shardingsphere.test.MockedDataSource" />
- <bean id="ds_1_slave_1" class="org.apache.shardingsphere.test.MockedDataSource" />
+ <bean id="ds_0_master" class="org.apache.shardingsphere.jdbc.test.MockedDataSource" />
+ <bean id="ds_0_slave_0" class="org.apache.shardingsphere.jdbc.test.MockedDataSource" />
+ <bean id="ds_0_slave_1" class="org.apache.shardingsphere.jdbc.test.MockedDataSource" />
+ <bean id="ds_1_master" class="org.apache.shardingsphere.jdbc.test.MockedDataSource" />
+ <bean id="ds_1_slave_0" class="org.apache.shardingsphere.jdbc.test.MockedDataSource" />
+ <bean id="ds_1_slave_1" class="org.apache.shardingsphere.jdbc.test.MockedDataSource" />
<sharding:sharding-algorithm id="dataSourceShardingAlgorithm" type="INLINE">
<props>
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/pom.xml b/shardingsphere-proxy/shardingsphere-proxy-backend/pom.xml
index e2c2b1d..b3d3d95 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/pom.xml
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/pom.xml
@@ -122,6 +122,13 @@
<artifactId>shardingsphere-transaction-xa-core</artifactId>
<version>${project.parent.version}</version>
</dependency>
+
+ <dependency>
+ <groupId>org.apache.shardingsphere</groupId>
+ <artifactId>shardingsphere-test</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/datasource/decorator/HikariJDBCParameterDecorator.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/datasource/decorator/HikariJDBCParameterDecorator.java
index 0e08d15..d76835a 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/datasource/decorator/HikariJDBCParameterDecorator.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/datasource/decorator/HikariJDBCParameterDecorator.java
@@ -26,7 +26,7 @@ import org.apache.shardingsphere.infra.config.datasource.JDBCParameterDecorator;
public final class HikariJDBCParameterDecorator implements JDBCParameterDecorator<HikariDataSource> {
@Override
- public void decorate(final HikariDataSource dataSource) {
+ public HikariDataSource decorate(final HikariDataSource dataSource) {
dataSource.getDataSourceProperties().setProperty("useServerPrepStmts", Boolean.TRUE.toString());
dataSource.getDataSourceProperties().setProperty("cachePrepStmts", Boolean.TRUE.toString());
dataSource.getDataSourceProperties().setProperty("prepStmtCacheSize", "250");
@@ -39,6 +39,7 @@ public final class HikariJDBCParameterDecorator implements JDBCParameterDecorato
dataSource.getDataSourceProperties().setProperty("maintainTimeStats", Boolean.FALSE.toString());
dataSource.getDataSourceProperties().setProperty("netTimeoutForStreamingResults", "0");
dataSource.getDataSourceProperties().setProperty("tinyInt1isBit", Boolean.FALSE.toString());
+ return new HikariDataSource(dataSource);
}
@Override
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/datasource/factory/JDBCRawBackendDataSourceFactory.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/datasource/factory/JDBCRawBackendDataSourceFactory.java
index 2405c82..65c7f8a 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/datasource/factory/JDBCRawBackendDataSourceFactory.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/datasource/factory/JDBCRawBackendDataSourceFactory.java
@@ -51,7 +51,7 @@ public final class JDBCRawBackendDataSourceFactory implements JDBCBackendDataSou
return INSTANCE;
}
- @SuppressWarnings("unchecked")
+ @SuppressWarnings({"unchecked", "rawtypes"})
@Override
public DataSource build(final String dataSourceName, final DataSourceParameter dataSourceParameter) {
HikariConfig config = new HikariConfig();
@@ -68,8 +68,8 @@ public final class JDBCRawBackendDataSourceFactory implements JDBCBackendDataSou
config.setMinimumIdle(dataSourceParameter.getMinPoolSize());
config.setReadOnly(dataSourceParameter.isReadOnly());
DataSource result = new HikariDataSource(config);
- findJDBCParameterDecorator(result).ifPresent(decorator -> decorator.decorate(result));
- return result;
+ Optional<JDBCParameterDecorator> decorator = findJDBCParameterDecorator(result);
+ return decorator.isPresent() ? decorator.get().decorate(result) : result;
}
private void validateDriverClassName(final String driverClassName) {
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/datasource/decorator/HikariJDBCParameterDecoratorTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/datasource/decorator/HikariJDBCParameterDecoratorTest.java
index 037de48..9e53773 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/datasource/decorator/HikariJDBCParameterDecoratorTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/datasource/decorator/HikariJDBCParameterDecoratorTest.java
@@ -35,9 +35,11 @@ public final class HikariJDBCParameterDecoratorTest {
@Test
public void assertDecoratedHikariDataSource() {
- HikariDataSource hikariDataSource = new HikariDataSource();
- new HikariJDBCParameterDecorator().decorate(hikariDataSource);
- Properties props = hikariDataSource.getDataSourceProperties();
+ HikariDataSource dataSource = new HikariDataSource();
+ dataSource.setDriverClassName("org.apache.shardingsphere.jdbc.test.MockedDriver");
+ dataSource.setJdbcUrl("mock:jdbc");
+ HikariDataSource actual = new HikariJDBCParameterDecorator().decorate(dataSource);
+ Properties props = actual.getDataSourceProperties();
assertThat(props.getProperty("useServerPrepStmts"), is(Boolean.TRUE.toString()));
assertThat(props.getProperty("cachePrepStmts"), is(Boolean.TRUE.toString()));
assertThat(props.getProperty("prepStmtCacheSize"), is("250"));
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/resources/logback-test.xml b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/resources/logback-test.xml
new file mode 100644
index 0000000..acb15fd
--- /dev/null
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/resources/logback-test.xml
@@ -0,0 +1,33 @@
+<?xml version="1.0"?>
+<!--
+ ~ Licensed to the Apache Software Foundation (ASF) under one or more
+ ~ contributor license agreements. See the NOTICE file distributed with
+ ~ this work for additional information regarding copyright ownership.
+ ~ The ASF licenses this file to You under the Apache License, Version 2.0
+ ~ (the "License"); you may not use this file except in compliance with
+ ~ the License. You may obtain a copy of the License at
+ ~
+ ~ http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing, software
+ ~ distributed under the License is distributed on an "AS IS" BASIS,
+ ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ ~ See the License for the specific language governing permissions and
+ ~ limitations under the License.
+ -->
+
+<configuration>
+ <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
+ <encoder>
+ <pattern>[%-5level] %d{HH:mm:ss.SSS} [%thread] %logger{36} - %msg%n</pattern>
+ </encoder>
+ </appender>
+ <logger name="org.apache.shardingsphere" level="warn" additivity="false">
+ <appender-ref ref="console"/>
+ </logger>
+
+ <root>
+ <level value="error" />
+ <appender-ref ref="console" />
+ </root>
+</configuration>
diff --git a/shardingsphere-test/src/main/java/org/apache/shardingsphere/test/MockedDataSource.java b/shardingsphere-test/src/main/java/org/apache/shardingsphere/jdbc/test/MockedDataSource.java
similarity index 98%
rename from shardingsphere-test/src/main/java/org/apache/shardingsphere/test/MockedDataSource.java
rename to shardingsphere-test/src/main/java/org/apache/shardingsphere/jdbc/test/MockedDataSource.java
index 1e0569e..94bd211 100644
--- a/shardingsphere-test/src/main/java/org/apache/shardingsphere/test/MockedDataSource.java
+++ b/shardingsphere-test/src/main/java/org/apache/shardingsphere/jdbc/test/MockedDataSource.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.test;
+package org.apache.shardingsphere.jdbc.test;
import javax.sql.DataSource;
import java.io.PrintWriter;
diff --git a/shardingsphere-test/src/main/java/org/apache/shardingsphere/jdbc/test/MockedDriver.java b/shardingsphere-test/src/main/java/org/apache/shardingsphere/jdbc/test/MockedDriver.java
new file mode 100644
index 0000000..ef1a8aa
--- /dev/null
+++ b/shardingsphere-test/src/main/java/org/apache/shardingsphere/jdbc/test/MockedDriver.java
@@ -0,0 +1,68 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.jdbc.test;
+
+import java.sql.Connection;
+import java.sql.Driver;
+import java.sql.DriverPropertyInfo;
+import java.util.Properties;
+import java.util.logging.Logger;
+
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+
+/**
+ * Mocked driver.
+ */
+public final class MockedDriver implements Driver {
+
+ @Override
+ public Connection connect(final String url, final Properties info) {
+ return mock(Connection.class, RETURNS_DEEP_STUBS);
+ }
+
+ @Override
+ public boolean acceptsURL(final String url) {
+ return url.startsWith("mock:jdbc");
+ }
+
+ @Override
+ public DriverPropertyInfo[] getPropertyInfo(final String url, final Properties info) {
+ return new DriverPropertyInfo[0];
+ }
+
+ @Override
+ public int getMajorVersion() {
+ return 0;
+ }
+
+ @Override
+ public int getMinorVersion() {
+ return 0;
+ }
+
+ @Override
+ public boolean jdbcCompliant() {
+ return true;
+ }
+
+ @Override
+ public Logger getParentLogger() {
+ return mock(Logger.class);
+ }
+}