Skip to content

Commit

Permalink
Fix issue for HikariJDBCParameterDecorator can not append JDBC parame…
Browse files Browse the repository at this point in the history
…ters (apache#7290)

* Remove useless fixture

* Refactor BootstrapArguments

* Refactor BootstrapArgumentsTest

* Fix issue for HikariJDBCParameterDecorator can not append JDBC parameters
  • Loading branch information
terrymanu authored Sep 7, 2020
1 parent db56f7f commit 4ea7510
Show file tree
Hide file tree
Showing 14 changed files with 135 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private static Collection<Method> findAllGetterMethods(final Class<?> clazz) {
*
* @return data source
*/
@SuppressWarnings("unchecked")
@SuppressWarnings({"unchecked", "rawtypes"})
@SneakyThrows(ReflectiveOperationException.class)
public DataSource createDataSource() {
DataSource result = (DataSource) Class.forName(dataSourceClassName).getConstructor().newInstance();
Expand All @@ -116,8 +116,8 @@ public DataSource createDataSource() {
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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
7 changes: 7 additions & 0 deletions shardingsphere-proxy/shardingsphere-proxy-backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
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");
Expand All @@ -39,6 +39,7 @@ public void decorate(final HikariDataSource dataSource) {
dataSource.getDataSourceProperties().setProperty("maintainTimeStats", Boolean.FALSE.toString());
dataSource.getDataSourceProperties().setProperty("netTimeoutForStreamingResults", "0");
dataSource.getDataSourceProperties().setProperty("tinyInt1isBit", Boolean.FALSE.toString());
return new HikariDataSource(dataSource);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static JDBCBackendDataSourceFactory getInstance() {
return INSTANCE;
}

@SuppressWarnings("unchecked")
@SuppressWarnings({"unchecked", "rawtypes"})
@Override
public DataSource build(final String dataSourceName, final DataSourceParameter dataSourceParameter) {
HikariConfig config = new HikariConfig();
Expand All @@ -68,8 +68,8 @@ public DataSource build(final String dataSourceName, final DataSourceParameter d
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ public void assertGetTypeResultIsHikariDataSource() {

@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"));
Expand Down
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 4ea7510

Please sign in to comment.