提交 53c5d5cb 编写于 作者: T terrymanu

refactor AbstractDataSourceAdapter

上级 5c61f05e
......@@ -21,6 +21,7 @@ import com.dangdang.ddframe.rdb.sharding.config.common.api.ShardingRuleBuilder;
import com.dangdang.ddframe.rdb.sharding.config.common.api.config.ShardingRuleConfig;
import com.dangdang.ddframe.rdb.sharding.jdbc.core.datasource.ShardingDataSource;
import java.sql.SQLException;
import java.util.Properties;
/**
......@@ -30,7 +31,7 @@ import java.util.Properties;
*/
public class SpringShardingDataSource extends ShardingDataSource {
public SpringShardingDataSource(final ShardingRuleConfig shardingRuleConfig, final Properties props) {
public SpringShardingDataSource(final ShardingRuleConfig shardingRuleConfig, final Properties props) throws SQLException {
super(new ShardingRuleBuilder(shardingRuleConfig).build(), props);
}
}
......@@ -29,6 +29,7 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.SQLException;
import java.util.Map;
/**
......@@ -38,15 +39,15 @@ import java.util.Map;
*/
public class YamlShardingDataSource extends ShardingDataSource {
public YamlShardingDataSource(final File yamlFile) throws IOException {
public YamlShardingDataSource(final File yamlFile) throws IOException, SQLException {
super(new ShardingRuleBuilder(yamlFile.getName(), unmarshal(yamlFile)).build(), unmarshal(yamlFile).getProps());
}
public YamlShardingDataSource(final Map<String, DataSource> dataSource, final File yamlFile) throws IOException {
public YamlShardingDataSource(final Map<String, DataSource> dataSource, final File yamlFile) throws IOException, SQLException {
super(new ShardingRuleBuilder(yamlFile.getName(), dataSource, unmarshal(yamlFile)).build(), unmarshal(yamlFile).getProps());
}
public YamlShardingDataSource(final String logRoot, final byte[] yamlByteArray) throws IOException {
public YamlShardingDataSource(final String logRoot, final byte[] yamlByteArray) throws IOException, SQLException {
super(new ShardingRuleBuilder(logRoot, unmarshal(yamlByteArray)).build(), unmarshal(yamlByteArray).getProps());
}
......
......@@ -33,6 +33,7 @@ import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.net.URISyntaxException;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
......@@ -47,7 +48,7 @@ import static org.junit.Assert.fail;
public class YamlShardingDataSourceTest {
@Test
public void assertAll() throws IOException, NoSuchFieldException, IllegalAccessException, URISyntaxException {
public void assertAll() throws IOException, NoSuchFieldException, IllegalAccessException, URISyntaxException, SQLException {
ShardingRule shardingRule = getShardingRule("/config/config-all.yaml");
assertThat(shardingRule.getTableRules().size(), is(3));
assertThat(shardingRule.getBindingTableRules().size(), is(1));
......@@ -56,7 +57,7 @@ public class YamlShardingDataSourceTest {
}
@Test
public void assertMin() throws IOException, NoSuchFieldException, IllegalAccessException, URISyntaxException {
public void assertMin() throws IOException, ReflectiveOperationException, URISyntaxException, SQLException {
Map<String, DataSource> dataSourceMap = new HashMap<>(1);
dataSourceMap.put("ds", createDataSource());
ShardingRule shardingRule = getShardingRule(dataSourceMap, "/config/config-min.yaml");
......@@ -64,7 +65,7 @@ public class YamlShardingDataSourceTest {
}
@Test
public void assertDynamic() throws IOException, NoSuchFieldException, IllegalAccessException, URISyntaxException {
public void assertDynamic() throws IOException, ReflectiveOperationException, URISyntaxException, SQLException {
Map<String, DataSource> dataSourceMap = new HashMap<>(1);
dataSourceMap.put("ds", createDataSource());
ShardingRule shardingRule = getShardingRule(dataSourceMap, "/config/config-dynamic.yaml");
......@@ -91,12 +92,12 @@ public class YamlShardingDataSourceTest {
}
@Test(expected = IllegalArgumentException.class)
public void assertClassNotFound() throws IOException, NoSuchFieldException, IllegalAccessException, URISyntaxException {
public void assertClassNotFound() throws IOException, NoSuchFieldException, IllegalAccessException, URISyntaxException, SQLException {
getShardingRule("/config/config-classNotFound.yaml");
}
@Test(expected = IllegalArgumentException.class)
public void assertBindingError() throws IOException, NoSuchFieldException, IllegalAccessException, URISyntaxException {
public void assertBindingError() throws IOException, ReflectiveOperationException, URISyntaxException, SQLException {
Map<String, DataSource> dataSourceMap = new HashMap<>(1);
dataSourceMap.put("ds", createDataSource());
ShardingRule shardingRule = getShardingRule(dataSourceMap, "/config/config-bindingError.yaml");
......@@ -105,11 +106,11 @@ public class YamlShardingDataSourceTest {
}
}
private ShardingRule getShardingRule(final String fileName) throws NoSuchFieldException, IllegalAccessException, URISyntaxException, IOException {
private ShardingRule getShardingRule(final String fileName) throws NoSuchFieldException, IllegalAccessException, URISyntaxException, IOException, SQLException {
return getShardingRule(new YamlShardingDataSource(new File(getClass().getResource(fileName).toURI())));
}
private ShardingRule getShardingRule(final Map<String, DataSource> dataSourceMap, final String fileName) throws NoSuchFieldException, IllegalAccessException, URISyntaxException, IOException {
private ShardingRule getShardingRule(final Map<String, DataSource> dataSourceMap, final String fileName) throws ReflectiveOperationException, URISyntaxException, IOException, SQLException {
return getShardingRule(new YamlShardingDataSource(dataSourceMap, new File(getClass().getResource(fileName).toURI())));
}
......
......@@ -23,6 +23,7 @@ import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import javax.sql.DataSource;
import java.sql.SQLException;
/**
* Master-slave data source factory.
......@@ -42,8 +43,9 @@ public final class MasterSlaveDataSourceFactory {
* @param slaveDataSource data source for slave
* @param otherSlaveDataSources other data sources for slave
* @return master-slave data source
* @throws SQLException SQL exception
*/
public static DataSource createDataSource(final String name, final DataSource masterDataSource, final DataSource slaveDataSource, final DataSource... otherSlaveDataSources) {
public static DataSource createDataSource(final String name, final DataSource masterDataSource, final DataSource slaveDataSource, final DataSource... otherSlaveDataSources) throws SQLException {
return new MasterSlaveDataSource(name, masterDataSource, Lists.asList(slaveDataSource, otherSlaveDataSources));
}
}
......@@ -23,6 +23,7 @@ import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Properties;
/**
......@@ -38,8 +39,9 @@ public final class ShardingDataSourceFactory {
*
* @param shardingRule rule for databases and tables sharding
* @return sharding data source
* @throws SQLException SQL exception
*/
public static DataSource createDataSource(final ShardingRule shardingRule) {
public static DataSource createDataSource(final ShardingRule shardingRule) throws SQLException {
return new ShardingDataSource(shardingRule);
}
......@@ -49,8 +51,9 @@ public final class ShardingDataSourceFactory {
* @param shardingRule rule for databases and tables sharding
* @param props properties for data source
* @return sharding data source
* @throws SQLException SQL exception
*/
public static DataSource createDataSource(final ShardingRule shardingRule, final Properties props) {
public static DataSource createDataSource(final ShardingRule shardingRule, final Properties props) throws SQLException {
return new ShardingDataSource(shardingRule, props);
}
}
......@@ -17,7 +17,6 @@
package com.dangdang.ddframe.rdb.sharding.jdbc.adapter;
import com.dangdang.ddframe.rdb.sharding.exception.ShardingJdbcException;
import com.dangdang.ddframe.rdb.sharding.jdbc.unsupported.AbstractUnsupportedOperationDataSource;
import com.google.common.base.Preconditions;
import lombok.Getter;
......@@ -42,11 +41,11 @@ public abstract class AbstractDataSourceAdapter extends AbstractUnsupportedOpera
private PrintWriter logWriter = new PrintWriter(System.out);
public AbstractDataSourceAdapter(final Collection<DataSource> dataSources) {
public AbstractDataSourceAdapter(final Collection<DataSource> dataSources) throws SQLException {
databaseProductName = getDatabaseProductName(dataSources);
}
private String getDatabaseProductName(final Collection<DataSource> dataSources) {
private String getDatabaseProductName(final Collection<DataSource> dataSources) throws SQLException {
String result = null;
for (DataSource each : dataSources) {
String databaseProductName;
......@@ -55,9 +54,6 @@ public abstract class AbstractDataSourceAdapter extends AbstractUnsupportedOpera
} else {
try (Connection connection = each.getConnection()) {
databaseProductName = connection.getMetaData().getDatabaseProductName();
// TODO throw
} catch (final SQLException ex) {
throw new ShardingJdbcException(ex);
}
}
Preconditions.checkState(null == result || result.equals(databaseProductName), String.format("Database type inconsistent with '%s' and '%s'", result, databaseProductName));
......
......@@ -58,7 +58,7 @@ public final class MasterSlaveDataSource extends AbstractDataSourceAdapter {
private final SlaveLoadBalanceStrategy slaveLoadBalanceStrategy = new RoundRobinSlaveLoadBalanceStrategy();
public MasterSlaveDataSource(final String name, final DataSource masterDataSource, final List<DataSource> slaveDataSources) {
public MasterSlaveDataSource(final String name, final DataSource masterDataSource, final List<DataSource> slaveDataSources) throws SQLException {
super(getAllDataSources(masterDataSource, slaveDataSources));
this.name = name;
this.masterDataSource = masterDataSource;
......
......@@ -42,11 +42,11 @@ public class ShardingDataSource extends AbstractDataSourceAdapter implements Aut
private final ShardingContext shardingContext;
public ShardingDataSource(final ShardingRule shardingRule) {
public ShardingDataSource(final ShardingRule shardingRule) throws SQLException {
this(shardingRule, new Properties());
}
public ShardingDataSource(final ShardingRule shardingRule, final Properties props) {
public ShardingDataSource(final ShardingRule shardingRule, final Properties props) throws SQLException {
super(shardingRule.getDataSourceRule().getDataSources());
shardingProperties = new ShardingProperties(null == props ? new Properties() : props);
int executorSize = shardingProperties.getValue(ShardingPropertiesConstant.EXECUTOR_SIZE);
......
......@@ -85,19 +85,19 @@ public abstract class AbstractSQLAssertTest extends AbstractSQLTest {
protected abstract ShardingTestStrategy getShardingStrategy();
protected abstract Map<DatabaseType, ShardingDataSource> getShardingDataSources();
protected abstract Map<DatabaseType, ShardingDataSource> getShardingDataSources() throws SQLException;
@Test
public void assertWithPreparedStatement() {
public void assertWithPreparedStatement() throws SQLException {
execute(true);
}
@Test
public void assertWithStatement() {
public void assertWithStatement() throws SQLException {
execute(false);
}
private void execute(final boolean isPreparedStatement) {
private void execute(final boolean isPreparedStatement) throws SQLException {
for (Map.Entry<DatabaseType, ShardingDataSource> each : getShardingDataSources().entrySet()) {
if (getCurrentDatabaseType() == each.getKey()) {
try {
......
......@@ -33,6 +33,7 @@ import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
......@@ -52,7 +53,7 @@ public abstract class AbstractShardingJDBCDatabaseAndTableTest extends AbstractS
}
@Before
public void initShardingDataSources() {
public void initShardingDataSources() throws SQLException {
Map<DatabaseType, Map<String, DataSource>> dataSourceMap = createDataSourceMap();
for (Map.Entry<DatabaseType, Map<String, DataSource>> each : dataSourceMap.entrySet()) {
DataSourceRule dataSourceRule = new DataSourceRule(each.getValue());
......
......@@ -35,6 +35,7 @@ import org.junit.AfterClass;
import org.junit.runners.Parameterized;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
......@@ -78,7 +79,7 @@ public class NullableShardingTableOnlyTest extends AbstractSQLAssertTest {
}
@Override
protected final Map<DatabaseType, ShardingDataSource> getShardingDataSources() {
protected final Map<DatabaseType, ShardingDataSource> getShardingDataSources() throws SQLException {
if (!shardingDataSources.isEmpty() && !isShutdown) {
return shardingDataSources;
}
......
......@@ -33,6 +33,7 @@ import com.dangdang.ddframe.rdb.sharding.jdbc.core.datasource.ShardingDataSource
import org.junit.AfterClass;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
......@@ -70,7 +71,7 @@ public class ShardingDatabaseAndTableDynamicTest extends AbstractSQLAssertTest {
}
@Override
protected final Map<DatabaseType, ShardingDataSource> getShardingDataSources() {
protected final Map<DatabaseType, ShardingDataSource> getShardingDataSources() throws SQLException {
if (!shardingDataSources.isEmpty() && !isShutdown) {
return shardingDataSources;
}
......
......@@ -33,6 +33,7 @@ import com.dangdang.ddframe.rdb.sharding.jdbc.core.datasource.ShardingDataSource
import org.junit.AfterClass;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
......@@ -70,7 +71,7 @@ public class ShardingDatabaseAndTableTest extends AbstractSQLAssertTest {
}
@Override
protected final Map<DatabaseType, ShardingDataSource> getShardingDataSources() {
protected final Map<DatabaseType, ShardingDataSource> getShardingDataSources() throws SQLException {
if (!shardingDataSources.isEmpty() && !isShutdown) {
return shardingDataSources;
}
......
......@@ -34,6 +34,7 @@ import com.dangdang.ddframe.rdb.sharding.keygen.fixture.IncrementKeyGenerator;
import org.junit.AfterClass;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
......@@ -71,7 +72,7 @@ public class ShardingDatabaseOnlyTest extends AbstractSQLAssertTest {
}
@Override
protected Map<DatabaseType, ShardingDataSource> getShardingDataSources() {
protected Map<DatabaseType, ShardingDataSource> getShardingDataSources() throws SQLException {
if (!shardingDataSources.isEmpty() && !isShutdown) {
return shardingDataSources;
}
......
......@@ -36,6 +36,7 @@ import org.junit.After;
import org.junit.AfterClass;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
......@@ -83,7 +84,7 @@ public class ShardingMasterSlaveTest extends AbstractSQLAssertTest {
}
@Override
protected final Map<DatabaseType, ShardingDataSource> getShardingDataSources() {
protected final Map<DatabaseType, ShardingDataSource> getShardingDataSources() throws SQLException {
if (!shardingDataSources.isEmpty() && !isShutdown) {
return shardingDataSources;
}
......
......@@ -36,6 +36,7 @@ import org.junit.Before;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Arrays;
import java.util.Collections;
......@@ -62,7 +63,7 @@ public class ShardingTableOnlyTest extends AbstractSQLAssertTest {
}
@Override
protected final Map<DatabaseType, ShardingDataSource> getShardingDataSources() {
protected final Map<DatabaseType, ShardingDataSource> getShardingDataSources() throws SQLException {
if (!shardingDataSources.isEmpty()) {
return shardingDataSources;
}
......@@ -103,7 +104,7 @@ public class ShardingTableOnlyTest extends AbstractSQLAssertTest {
}
@Before
public void initDdlTables() {
public void initDDLTables() throws SQLException {
if (getSql().startsWith("ALTER") || getSql().startsWith("TRUNCATE") || getSql().startsWith("DROP")) {
if (getSql().contains("TEMP")) {
executeSql("CREATE TEMPORARY TABLE t_log(id int, status varchar(10))");
......@@ -114,7 +115,7 @@ public class ShardingTableOnlyTest extends AbstractSQLAssertTest {
}
@After
public void cleanupDdlTables() {
public void cleanupDdlTables() throws SQLException {
if (getSql().contains("TEMP") && DatabaseType.Oracle != getCurrentDatabaseType()) {
return;
}
......@@ -123,7 +124,7 @@ public class ShardingTableOnlyTest extends AbstractSQLAssertTest {
}
}
private void executeSql(final String sql) {
private void executeSql(final String sql) throws SQLException {
for (Map.Entry<DatabaseType, ShardingDataSource> each : getShardingDataSources().entrySet()) {
if (getCurrentDatabaseType() == each.getKey()) {
try (Connection conn = each.getValue().getConnection();
......
......@@ -68,7 +68,7 @@ public abstract class AbstractHintTest extends AbstractSQLTest {
"integrate/dataset/db/init/db_9.xml");
}
protected final Map<DatabaseType, ShardingDataSource> getShardingDataSources() {
protected final Map<DatabaseType, ShardingDataSource> getShardingDataSources() throws SQLException {
if (!shardingDataSources.isEmpty() && !isShutdown) {
return shardingDataSources;
}
......
......@@ -118,7 +118,7 @@ public final class ShardingDataSourceTest {
assertThat(createShardingDataSource(dataSourceMap).getConnection().getConnection("ds", SQLType.DQL), is(dataSource.getConnection()));
}
private ShardingDataSource createShardingDataSource(final Map<String, DataSource> dataSourceMap) {
private ShardingDataSource createShardingDataSource(final Map<String, DataSource> dataSourceMap) throws SQLException {
DataSourceRule dataSourceRule = new DataSourceRule(dataSourceMap);
TableRule tableRule = TableRule.builder("logicTable").actualTables(Arrays.asList("table_0", "table_1", "table_2")).dataSourceRule(dataSourceRule).build();
return new ShardingDataSource(ShardingRule.builder()
......
......@@ -101,7 +101,7 @@ public final class Main {
}
}
private static ShardingDataSource getShardingDataSource() {
private static ShardingDataSource getShardingDataSource() throws SQLException {
DataSourceRule dataSourceRule = new DataSourceRule(createDataSourceMap());
TableRule orderTableRule = TableRule.builder("t_order").actualTables(Arrays.asList("t_order_0", "t_order_1")).dataSourceRule(dataSourceRule).build();
TableRule orderItemTableRule = TableRule.builder("t_order_item").actualTables(Arrays.asList("t_order_item_0", "t_order_item_1")).dataSourceRule(dataSourceRule).build();
......@@ -112,7 +112,7 @@ public final class Main {
return new ShardingDataSource(shardingRule);
}
private static Map<String, DataSource> createDataSourceMap() {
private static Map<String, DataSource> createDataSourceMap() throws SQLException {
Map<String, DataSource> result = new HashMap<>(2);
result.put("ds_0", MasterSlaveDataSourceFactory.createDataSource("ds_0", createDataSource("ds_0_master"), createDataSource("ds_0_slave_0"), createDataSource("ds_0_slave_1")));
result.put("ds_1", MasterSlaveDataSourceFactory.createDataSource("ds_1", createDataSource("ds_1_master"), createDataSource("ds_1_slave_0"), createDataSource("ds_1_slave_1")));
......
......@@ -80,7 +80,7 @@ public final class Main {
}
}
private static DataSource getShardingDataSource() {
private static DataSource getShardingDataSource() throws SQLException {
DataSourceRule dataSourceRule = new DataSourceRule(createDataSourceMap());
TableRule orderTableRule = TableRule.builder("t_order").actualTables(Arrays.asList("t_order_0", "t_order_1")).dataSourceRule(dataSourceRule).build();
TableRule orderItemTableRule = TableRule.builder("t_order_item").actualTables(Arrays.asList("t_order_item_0", "t_order_item_1")).dataSourceRule(dataSourceRule).build();
......
......@@ -119,7 +119,7 @@ public final class Main {
}
}
private static ShardingDataSource getShardingDataSource() {
private static ShardingDataSource getShardingDataSource() throws SQLException {
DataSourceRule dataSourceRule = new DataSourceRule(createDataSourceMap());
TableRule orderTableRule = TableRule.builder("t_order").actualTables(Arrays.asList("t_order_0", "t_order_1")).dataSourceRule(dataSourceRule).build();
TableRule orderItemTableRule = TableRule.builder("t_order_item").actualTables(Arrays.asList("t_order_item_0", "t_order_item_1")).dataSourceRule(dataSourceRule).build();
......
......@@ -47,7 +47,7 @@ public abstract class AbstractSoftTransactionIntegrationTest {
prepareEnv();
}
private void prepareEnv() {
private void prepareEnv() throws SQLException {
DataSourceRule dataSourceRule = new DataSourceRule(createDataSourceMap());
TableRule tableRule = TableRule.builder("transaction_test").dataSourceRule(dataSourceRule).build();
ShardingRule shardingRule = ShardingRule.builder().dataSourceRule(dataSourceRule).tableRules(Lists.newArrayList(tableRule)).build();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册