提交 d49f485b 编写于 作者: T terrymanu

for #768, remove master-slave logic from ShardingConnection

上级 31b2797a
......@@ -68,6 +68,7 @@ public final class ShardingRule {
public ShardingRule(final ShardingRuleConfiguration shardingRuleConfig, final Collection<String> dataSourceNames) {
Preconditions.checkNotNull(dataSourceNames, "Data sources cannot be null.");
Preconditions.checkArgument(!dataSourceNames.isEmpty(), "Data sources cannot be empty.");
processDataSourceNamesWithMasterSlave(shardingRuleConfig.getMasterSlaveRuleConfigs(), dataSourceNames);
this.dataSourceNames = new LinkedHashSet<>(dataSourceNames);
defaultDataSourceName = getDefaultDataSourceName(dataSourceNames, shardingRuleConfig.getDefaultDataSourceName());
tableRules = new LinkedList<>();
......@@ -91,6 +92,27 @@ public final class ShardingRule {
}
}
private Collection<String> processDataSourceNamesWithMasterSlave(final Collection<MasterSlaveRuleConfiguration> masterSlaveRuleConfigs, final Collection<String> dataSourceNames) {
Collection<String> result = new LinkedHashSet<>(dataSourceNames);
for (MasterSlaveRuleConfiguration each : masterSlaveRuleConfigs) {
processDataSourceNamesWithMasterSlave(each, result);
}
return result;
}
private void processDataSourceNamesWithMasterSlave(final MasterSlaveRuleConfiguration masterSlaveRuleConfig, final Collection<String> dataSourceNames) {
Collection<String> toBeRemoved = new LinkedHashSet<>(masterSlaveRuleConfig.getSlaveDataSourceNames().size(), 1);
for (String each : dataSourceNames) {
if (masterSlaveRuleConfig.getMasterDataSourceName().equals(each) || masterSlaveRuleConfig.getSlaveDataSourceNames().contains(each)) {
toBeRemoved.add(each);
}
}
if (!toBeRemoved.isEmpty()) {
dataSourceNames.removeAll(toBeRemoved);
dataSourceNames.add(masterSlaveRuleConfig.getName());
}
}
private String getDefaultDataSourceName(final Collection<String> dataSourceNames, final String defaultDataSourceName) {
if (1 == dataSourceNames.size()) {
return dataSourceNames.iterator().next();
......
......@@ -24,6 +24,7 @@ import io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource;
import io.shardingjdbc.core.jdbc.core.statement.MasterSlavePreparedStatement;
import io.shardingjdbc.core.jdbc.core.statement.MasterSlaveStatement;
import io.shardingjdbc.core.routing.router.masterslave.MasterSlaveRouter;
import io.shardingjdbc.core.routing.router.masterslave.MasterVisitedManager;
import lombok.RequiredArgsConstructor;
import javax.sql.DataSource;
......@@ -137,7 +138,7 @@ public final class MasterSlaveConnection extends AbstractConnectionAdapter {
@Override
public void close() throws SQLException {
HintManagerHolder.clear();
MasterSlaveDataSource.resetDMLFlag();
MasterVisitedManager.clear();
super.close();
}
}
......@@ -18,14 +18,13 @@
package io.shardingjdbc.core.jdbc.core.connection;
import com.google.common.base.Preconditions;
import io.shardingjdbc.core.constant.SQLType;
import io.shardingjdbc.core.hint.HintManagerHolder;
import io.shardingjdbc.core.jdbc.adapter.AbstractConnectionAdapter;
import io.shardingjdbc.core.jdbc.core.ShardingContext;
import io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource;
import io.shardingjdbc.core.jdbc.core.datasource.NamedDataSource;
import io.shardingjdbc.core.jdbc.core.statement.ShardingPreparedStatement;
import io.shardingjdbc.core.jdbc.core.statement.ShardingStatement;
import io.shardingjdbc.core.routing.router.masterslave.MasterVisitedManager;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
......@@ -88,29 +87,17 @@ public final class ShardingConnection extends AbstractConnectionAdapter {
* Get database connection via data source name.
*
* @param dataSourceName data source name
* @param sqlType SQL type
* @return all database connections via data source name
* @throws SQLException SQL exception
*/
public Connection getConnection(final String dataSourceName, final SQLType sqlType) throws SQLException {
public Connection getConnection(final String dataSourceName) throws SQLException {
if (getCachedConnections().containsKey(dataSourceName)) {
return getCachedConnections().get(dataSourceName);
}
DataSource dataSource = shardingContext.getDataSourceMap().get(dataSourceName);
Preconditions.checkState(null != dataSource, "Missing the rule of %s in DataSourceRule", dataSourceName);
String realDataSourceName;
if (dataSource instanceof MasterSlaveDataSource) {
NamedDataSource namedDataSource = ((MasterSlaveDataSource) dataSource).getDataSource(sqlType);
realDataSourceName = namedDataSource.getName();
if (getCachedConnections().containsKey(realDataSourceName)) {
return getCachedConnections().get(realDataSourceName);
}
dataSource = namedDataSource.getDataSource();
} else {
realDataSourceName = dataSourceName;
}
Connection result = dataSource.getConnection();
getCachedConnections().put(realDataSourceName, result);
getCachedConnections().put(dataSourceName, result);
replayMethodsInvocation(result);
return result;
}
......@@ -130,7 +117,7 @@ public final class ShardingConnection extends AbstractConnectionAdapter {
@Override
public DatabaseMetaData getMetaData() throws SQLException {
return getConnection(shardingContext.getDataSourceMap().keySet().iterator().next(), SQLType.DQL).getMetaData();
return getConnection(shardingContext.getDataSourceMap().keySet().iterator().next()).getMetaData();
}
@Override
......@@ -181,7 +168,7 @@ public final class ShardingConnection extends AbstractConnectionAdapter {
@Override
public void close() throws SQLException {
HintManagerHolder.clear();
MasterSlaveDataSource.resetDMLFlag();
MasterVisitedManager.clear();
super.close();
}
}
......@@ -98,13 +98,6 @@ public class MasterSlaveDataSource extends AbstractDataSourceAdapter {
return result;
}
/**
* reset DML flag.
*/
public static void resetDMLFlag() {
DML_FLAG.remove();
}
/**
* Get data source from master-slave data source.
*
......
......@@ -190,7 +190,7 @@ public final class ShardingPreparedStatement extends AbstractShardingPreparedSta
}
private PreparedStatement generatePreparedStatement(final SQLExecutionUnit sqlExecutionUnit) throws SQLException {
Connection connection = getConnection().getConnection(sqlExecutionUnit.getDataSource(), routeResult.getSqlStatement().getType());
Connection connection = getConnection().getConnection(sqlExecutionUnit.getDataSource());
return returnGeneratedKeys ? connection.prepareStatement(sqlExecutionUnit.getSqlUnit().getSql(), Statement.RETURN_GENERATED_KEYS)
: connection.prepareStatement(sqlExecutionUnit.getSqlUnit().getSql(), resultSetType, resultSetConcurrency, resultSetHoldability);
}
......
......@@ -207,7 +207,7 @@ public class ShardingStatement extends AbstractStatementAdapter {
if (SQLType.DDL == sqlType) {
connections = connection.getConnectionsForDDL(each.getDataSource());
} else {
connections = Collections.singletonList(connection.getConnection(each.getDataSource(), routeResult.getSqlStatement().getType()));
connections = Collections.singletonList(connection.getConnection(each.getDataSource()));
}
for (Connection connection : connections) {
Statement statement = connection.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
......
......@@ -23,7 +23,6 @@ import io.shardingjdbc.core.common.env.DatabaseEnvironment;
import io.shardingjdbc.core.common.env.ShardingJdbcDatabaseTester;
import io.shardingjdbc.core.common.env.ShardingTestStrategy;
import io.shardingjdbc.core.constant.DatabaseType;
import io.shardingjdbc.core.constant.SQLType;
import io.shardingjdbc.core.integrate.jaxb.SQLAssertData;
import io.shardingjdbc.core.integrate.jaxb.SQLShardingRule;
import io.shardingjdbc.core.jdbc.adapter.AbstractDataSourceAdapter;
......@@ -240,16 +239,16 @@ public abstract class AbstractSQLAssertTest extends AbstractSQLTest {
} else {
sqlAssertHelper.executeWithStatement(isExecute, abstractDataSourceAdapter, getParameters(data));
}
String dataSourceName = getDataSourceName(data.getExpected());
if (dataSourceName.contains("ms")) {
dataSourceName = dataSourceName.replace("ms", "dataSource_master");
}
try (Connection conn = abstractDataSourceAdapter instanceof MasterSlaveDataSource ? abstractDataSourceAdapter.getConnection()
: ((ShardingDataSource) abstractDataSourceAdapter).getConnection().getConnection(getDataSourceName(data.getExpected()), getSqlType())) {
: ((ShardingDataSource) abstractDataSourceAdapter).getConnection().getConnection(dataSourceName)) {
sqlAssertHelper.assertResult(conn, expectedDataSetFile);
}
}
private SQLType getSqlType() {
return ShardingTestStrategy.masterslave == getShardingStrategy() ? SQLType.DML : SQLType.DQL;
}
// TODO 标准化文件名
private String getDataSourceName(final String expected) {
String result = String.format(expected.split("/")[1].split(".xml")[0], getShardingStrategy().name());
......
......@@ -29,7 +29,6 @@ import io.shardingjdbc.core.hint.HintManagerHolder;
import io.shardingjdbc.core.integrate.fixture.PreciseModuloDatabaseShardingAlgorithm;
import io.shardingjdbc.core.integrate.fixture.RangeModuloDatabaseShardingAlgorithm;
import io.shardingjdbc.core.integrate.jaxb.SQLShardingRule;
import io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource;
import io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource;
import io.shardingjdbc.core.routing.router.masterslave.MasterVisitedManager;
import io.shardingjdbc.core.rule.ShardingRule;
......@@ -38,8 +37,8 @@ import org.junit.After;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
......@@ -92,22 +91,22 @@ public abstract class AbstractShardingMasterSlaveTest extends AbstractSQLAssertT
}
Map<DatabaseType, Map<String, DataSource>> dataSourceMap = createDataSourceMap();
for (Entry<DatabaseType, Map<String, DataSource>> entry : dataSourceMap.entrySet()) {
Map<String, DataSource> masterSlaveDataSourceMap = getMasterSlaveDataSourceMap(entry);
final ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
TableRuleConfiguration orderTableRuleConfig = new TableRuleConfiguration();
orderTableRuleConfig.setLogicTable("t_order");
orderTableRuleConfig.setLogicIndex("t_order_index");
List<String> orderActualDataNodes = new LinkedList<>();
for (String dataSourceName : masterSlaveDataSourceMap.keySet()) {
orderActualDataNodes.add(dataSourceName + ".t_order_${0..9}");
Collection<MasterSlaveRuleConfiguration> masterSlaveRuleConfigs = getMasterSlaveRuleConfigurations();
for (MasterSlaveRuleConfiguration each : masterSlaveRuleConfigs) {
orderActualDataNodes.add(each.getName() + ".t_order_${0..9}");
}
orderTableRuleConfig.setActualDataNodes(Joiner.on(",").join(orderActualDataNodes));
shardingRuleConfig.getTableRuleConfigs().add(orderTableRuleConfig);
TableRuleConfiguration orderItemTableRuleConfig = new TableRuleConfiguration();
orderItemTableRuleConfig.setLogicTable("t_order_item");
List<String> itemOrderActualDataNodes = new LinkedList<>();
for (String dataSourceName : masterSlaveDataSourceMap.keySet()) {
itemOrderActualDataNodes.add(dataSourceName + ".t_order_item_${0..9}");
for (MasterSlaveRuleConfiguration each : masterSlaveRuleConfigs) {
itemOrderActualDataNodes.add(each.getName() + ".t_order_item_${0..9}");
}
orderItemTableRuleConfig.setActualDataNodes(Joiner.on(",").join(itemOrderActualDataNodes));
shardingRuleConfig.getTableRuleConfigs().add(orderItemTableRuleConfig);
......@@ -125,44 +124,29 @@ public abstract class AbstractShardingMasterSlaveTest extends AbstractSQLAssertT
new StandardShardingStrategyConfiguration("t_order_item", new PreciseModuloDatabaseShardingAlgorithm(), new RangeModuloDatabaseShardingAlgorithm()));
shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(
new StandardShardingStrategyConfiguration("user_id", new PreciseModuloDatabaseShardingAlgorithm(), new RangeModuloDatabaseShardingAlgorithm()));
getShardingDataSources().put(entry.getKey(), new ShardingDataSource(masterSlaveDataSourceMap, new ShardingRule(shardingRuleConfig, masterSlaveDataSourceMap.keySet())));
shardingRuleConfig.getMasterSlaveRuleConfigs().addAll(masterSlaveRuleConfigs);
Map<String, DataSource> masterSlaveDataSourceMap = entry.getValue();
ShardingRule shardingRule = new ShardingRule(shardingRuleConfig, masterSlaveDataSourceMap.keySet());
getShardingDataSources().put(entry.getKey(), new ShardingDataSource(masterSlaveDataSourceMap, shardingRule));
}
return getShardingDataSources();
}
// TODO use MasterSlaveRuleConfiguration to generate data source map
private Map<String, DataSource> getMasterSlaveDataSourceMap(final Entry<DatabaseType, Map<String, DataSource>> each) throws SQLException {
Map<String, DataSource> masterSlaveDataSourceMap = each.getValue();
MasterSlaveDataSource masterSlaveDs0 = getMasterSlaveDataSource(masterSlaveDataSourceMap, "ms_0", "dataSource_master_0", "dataSource_slave_0");
MasterSlaveDataSource masterSlaveDs1 = getMasterSlaveDataSource(masterSlaveDataSourceMap, "ms_1", "dataSource_master_1", "dataSource_slave_1");
MasterSlaveDataSource masterSlaveDs2 = getMasterSlaveDataSource(masterSlaveDataSourceMap, "ms_2", "dataSource_master_2", "dataSource_slave_2");
MasterSlaveDataSource masterSlaveDs3 = getMasterSlaveDataSource(masterSlaveDataSourceMap, "ms_3", "dataSource_master_3", "dataSource_slave_3");
MasterSlaveDataSource masterSlaveDs4 = getMasterSlaveDataSource(masterSlaveDataSourceMap, "ms_4", "dataSource_master_4", "dataSource_slave_4");
MasterSlaveDataSource masterSlaveDs5 = getMasterSlaveDataSource(masterSlaveDataSourceMap, "ms_5", "dataSource_master_5", "dataSource_slave_5");
MasterSlaveDataSource masterSlaveDs6 = getMasterSlaveDataSource(masterSlaveDataSourceMap, "ms_6", "dataSource_master_6", "dataSource_slave_6");
MasterSlaveDataSource masterSlaveDs7 = getMasterSlaveDataSource(masterSlaveDataSourceMap, "ms_7", "dataSource_master_7", "dataSource_slave_7");
MasterSlaveDataSource masterSlaveDs8 = getMasterSlaveDataSource(masterSlaveDataSourceMap, "ms_8", "dataSource_master_8", "dataSource_slave_8");
MasterSlaveDataSource masterSlaveDs9 = getMasterSlaveDataSource(masterSlaveDataSourceMap, "ms_9", "dataSource_master_9", "dataSource_slave_9");
Map<String, DataSource> result = new HashMap<>(10);
result.put("ms_0", masterSlaveDs0);
result.put("ms_1", masterSlaveDs1);
result.put("ms_2", masterSlaveDs2);
result.put("ms_3", masterSlaveDs3);
result.put("ms_4", masterSlaveDs4);
result.put("ms_5", masterSlaveDs5);
result.put("ms_6", masterSlaveDs6);
result.put("ms_7", masterSlaveDs7);
result.put("ms_8", masterSlaveDs8);
result.put("ms_9", masterSlaveDs9);
private Collection<MasterSlaveRuleConfiguration> getMasterSlaveRuleConfigurations() {
Collection<MasterSlaveRuleConfiguration> result = new LinkedList<>();
result.add(new MasterSlaveRuleConfiguration("ms_0", "dataSource_master_0", Collections.singletonList("dataSource_slave_0")));
result.add(new MasterSlaveRuleConfiguration("ms_1", "dataSource_master_1", Collections.singletonList("dataSource_slave_1")));
result.add(new MasterSlaveRuleConfiguration("ms_2", "dataSource_master_2", Collections.singletonList("dataSource_slave_2")));
result.add(new MasterSlaveRuleConfiguration("ms_3", "dataSource_master_3", Collections.singletonList("dataSource_slave_3")));
result.add(new MasterSlaveRuleConfiguration("ms_4", "dataSource_master_4", Collections.singletonList("dataSource_slave_4")));
result.add(new MasterSlaveRuleConfiguration("ms_5", "dataSource_master_5", Collections.singletonList("dataSource_slave_5")));
result.add(new MasterSlaveRuleConfiguration("ms_6", "dataSource_master_6", Collections.singletonList("dataSource_slave_6")));
result.add(new MasterSlaveRuleConfiguration("ms_7", "dataSource_master_7", Collections.singletonList("dataSource_slave_7")));
result.add(new MasterSlaveRuleConfiguration("ms_8", "dataSource_master_8", Collections.singletonList("dataSource_slave_8")));
result.add(new MasterSlaveRuleConfiguration("ms_9", "dataSource_master_9", Collections.singletonList("dataSource_slave_9")));
return result;
}
private MasterSlaveDataSource getMasterSlaveDataSource(
final Map<String, DataSource> masterSlaveDataSourceMap, final String name, final String masterDataSourceName, final String slaveDataSourceName) throws SQLException {
return new MasterSlaveDataSource(
masterSlaveDataSourceMap, new MasterSlaveRuleConfiguration(name, masterDataSourceName, Collections.singleton(slaveDataSourceName)), Collections.<String, Object>emptyMap());
}
@After
public final void clearFlag() {
HintManagerHolder.clear();
......
......@@ -17,15 +17,14 @@
package io.shardingjdbc.core.integrate.type.sharding.hint.type;
import io.shardingjdbc.core.util.SQLPlaceholderUtil;
import io.shardingjdbc.core.constant.DatabaseType;
import io.shardingjdbc.core.constant.SQLType;
import io.shardingjdbc.core.integrate.sql.DatabaseTestSQL;
import io.shardingjdbc.core.integrate.type.sharding.hint.base.AbstractRoutingDatabaseOnlyWithHintTest;
import io.shardingjdbc.core.integrate.type.sharding.hint.helper.HintDatabaseShardingValueHelper;
import io.shardingjdbc.core.integrate.type.sharding.hint.helper.HintShardingValueHelper;
import io.shardingjdbc.core.jdbc.core.connection.ShardingConnection;
import io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource;
import io.shardingjdbc.core.util.SQLPlaceholderUtil;
import org.dbunit.DatabaseUnitException;
import org.junit.Before;
import org.junit.Test;
......@@ -166,9 +165,8 @@ public class RoutingDatabaseOnlyWithHintForDMLTest extends AbstractRoutingDataba
private void assertDataSet(final ShardingConnection connection, final DatabaseType type, final String expectedDataSetPattern, final String status) throws SQLException, DatabaseUnitException {
for (int i = 0; i < 10; i++) {
assertDataSet(String.format("integrate/dataset/sharding/hint/expect/%s/db_%s.xml", expectedDataSetPattern, i),
connection.getConnection(String.format("dataSource_db_%s", i), SQLType.DQL),
SQLPlaceholderUtil.replacePreparedStatement(DatabaseTestSQL.ASSERT_SELECT_WITH_STATUS_SQL), type, status);
assertDataSet(String.format("integrate/dataset/sharding/hint/expect/%s/db_%s.xml", expectedDataSetPattern, i),
connection.getConnection(String.format("dataSource_db_%s", i)), SQLPlaceholderUtil.replacePreparedStatement(DatabaseTestSQL.ASSERT_SELECT_WITH_STATUS_SQL), type, status);
}
}
}
......@@ -17,14 +17,13 @@
package io.shardingjdbc.core.integrate.type.sharding.hint.type;
import io.shardingjdbc.core.util.SQLPlaceholderUtil;
import io.shardingjdbc.core.constant.DatabaseType;
import io.shardingjdbc.core.constant.SQLType;
import io.shardingjdbc.core.integrate.sql.DatabaseTestSQL;
import io.shardingjdbc.core.integrate.type.sharding.hint.base.AbstractShardingDatabaseOnlyWithHintTest;
import io.shardingjdbc.core.integrate.type.sharding.hint.helper.HintShardingValueHelper;
import io.shardingjdbc.core.jdbc.core.connection.ShardingConnection;
import io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource;
import io.shardingjdbc.core.util.SQLPlaceholderUtil;
import org.dbunit.DatabaseUnitException;
import org.junit.Before;
import org.junit.Test;
......@@ -165,9 +164,8 @@ public final class ShardingDatabaseOnlyWithHintForDMLTest extends AbstractShardi
private void assertDataSet(final ShardingConnection connection, final DatabaseType type, final String expectedDataSetPattern, final String status) throws SQLException, DatabaseUnitException {
for (int i = 0; i < 10; i++) {
assertDataSet(String.format("integrate/dataset/sharding/hint/expect/%s/db_%s.xml", expectedDataSetPattern, i),
connection.getConnection(String.format("dataSource_db_%s", i), SQLType.DQL),
SQLPlaceholderUtil.replacePreparedStatement(DatabaseTestSQL.ASSERT_SELECT_WITH_STATUS_SQL), type, status);
assertDataSet(String.format("integrate/dataset/sharding/hint/expect/%s/db_%s.xml", expectedDataSetPattern, i),
connection.getConnection(String.format("dataSource_db_%s", i)), SQLPlaceholderUtil.replacePreparedStatement(DatabaseTestSQL.ASSERT_SELECT_WITH_STATUS_SQL), type, status);
}
}
}
......@@ -20,7 +20,6 @@ package io.shardingjdbc.core.jdbc.core.connection;
import io.shardingjdbc.core.api.config.MasterSlaveRuleConfiguration;
import io.shardingjdbc.core.api.config.ShardingRuleConfiguration;
import io.shardingjdbc.core.api.config.TableRuleConfiguration;
import io.shardingjdbc.core.constant.SQLType;
import io.shardingjdbc.core.fixture.TestDataSource;
import io.shardingjdbc.core.jdbc.core.ShardingContext;
import io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource;
......@@ -85,40 +84,19 @@ public final class ShardingConnectionTest {
}
@Test
public void assertGetConnectionSelectThenUpdate() throws Exception {
assertNotSame(connection.getConnection(DS_NAME, SQLType.DQL), connection.getConnection(DS_NAME, SQLType.DML));
public void assertGetConnectionFromCache() throws Exception {
assertSame(connection.getConnection(DS_NAME), connection.getConnection(DS_NAME));
}
@Test
public void assertGetConnectionUpdateThenSelect() throws Exception {
assertSame(connection.getConnection(DS_NAME, SQLType.DML), connection.getConnection(DS_NAME, SQLType.DQL));
}
@Test
public void assertGetConnectionBothSelect() throws Exception {
assertSame(connection.getConnection(DS_NAME, SQLType.DQL), connection.getConnection(DS_NAME, SQLType.DQL));
}
@Test
public void assertGetConnectionBothUpdate() throws Exception {
assertSame(connection.getConnection(DS_NAME, SQLType.DML), connection.getConnection(DS_NAME, SQLType.DML));
}
@Test
public void assertGetConnectionMixed() throws Exception {
Connection slaveConnection = connection.getConnection(DS_NAME, SQLType.DQL);
Connection masterConnection = connection.getConnection(DS_NAME, SQLType.DML);
assertNotSame(slaveConnection, masterConnection);
assertNotSame(slaveConnection, connection.getConnection(DS_NAME, SQLType.DQL));
assertNotSame(slaveConnection, connection.getConnection(DS_NAME, SQLType.DML));
assertSame(masterConnection, connection.getConnection(DS_NAME, SQLType.DQL));
assertSame(masterConnection, connection.getConnection(DS_NAME, SQLType.DML));
@Test(expected = IllegalStateException.class)
public void assertGetConnectionFailure() throws Exception {
connection.getConnection("not_exist");
}
@Test
public void assertRelease() throws Exception {
Connection conn = connection.getConnection(DS_NAME, SQLType.DML);
Connection conn = connection.getConnection(DS_NAME);
connection.release(conn);
assertNotSame(conn, connection.getConnection(DS_NAME, SQLType.DML));
assertNotSame(conn, connection.getConnection(DS_NAME));
}
}
......@@ -22,7 +22,6 @@ import io.shardingjdbc.core.api.MasterSlaveDataSourceFactory;
import io.shardingjdbc.core.api.config.MasterSlaveRuleConfiguration;
import io.shardingjdbc.core.api.config.ShardingRuleConfiguration;
import io.shardingjdbc.core.api.config.TableRuleConfiguration;
import io.shardingjdbc.core.constant.SQLType;
import io.shardingjdbc.core.constant.ShardingPropertiesConstant;
import io.shardingjdbc.core.executor.ExecutorEngine;
import io.shardingjdbc.core.rule.ShardingRule;
......@@ -141,7 +140,7 @@ public final class ShardingDataSourceTest {
DataSource dataSource = mockDataSource("H2");
Map<String, DataSource> dataSourceMap = new HashMap<>(1, 1);
dataSourceMap.put("ds", dataSource);
assertThat(createShardingDataSource(dataSourceMap).getConnection().getConnection("ds", SQLType.DQL), is(dataSource.getConnection()));
assertThat(createShardingDataSource(dataSourceMap).getConnection().getConnection("ds"), is(dataSource.getConnection()));
}
@Test
......
......@@ -36,6 +36,7 @@ public final class DBUnitUtil {
/**
* Get connection.
*
* @param dbEnv Database environment
* @param connection connection
* @return database connection
......
......@@ -17,13 +17,12 @@
package io.shardingjdbc.transaction.api.config;
import com.google.common.base.Optional;
import io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource;
import io.shardingjdbc.core.constant.SQLType;
import io.shardingjdbc.transaction.constants.TransactionLogDataSourceType;
import io.shardingjdbc.transaction.datasource.TransactionLogDataSource;
import io.shardingjdbc.transaction.datasource.impl.MemoryTransactionLogDataSource;
import io.shardingjdbc.transaction.datasource.impl.RdbTransactionLogDataSource;
import com.google.common.base.Optional;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
......@@ -80,7 +79,7 @@ public class SoftTransactionConfiguration {
if (!(targetDataSource instanceof ShardingDataSource)) {
return targetDataSource.getConnection();
}
return ((ShardingDataSource) targetDataSource).getConnection().getConnection(dataSourceName, SQLType.DQL);
return ((ShardingDataSource) targetDataSource).getConnection().getConnection(dataSourceName);
}
/**
......
......@@ -17,17 +17,16 @@
package io.shardingjdbc.transaction.bed.sync;
import io.shardingjdbc.core.constant.SQLType;
import com.google.common.eventbus.AllowConcurrentEvents;
import com.google.common.eventbus.Subscribe;
import io.shardingjdbc.core.executor.event.DMLExecutionEvent;
import io.shardingjdbc.transaction.api.SoftTransactionManager;
import io.shardingjdbc.transaction.api.config.SoftTransactionConfiguration;
import io.shardingjdbc.transaction.bed.BEDSoftTransaction;
import io.shardingjdbc.transaction.constants.SoftTransactionType;
import io.shardingjdbc.transaction.storage.TransactionLog;
import io.shardingjdbc.transaction.storage.TransactionLogStorage;
import io.shardingjdbc.transaction.storage.TransactionLogStorageFactory;
import com.google.common.eventbus.AllowConcurrentEvents;
import com.google.common.eventbus.Subscribe;
import io.shardingjdbc.transaction.constants.SoftTransactionType;
import lombok.extern.slf4j.Slf4j;
import java.sql.Connection;
......@@ -77,10 +76,10 @@ public final class BestEffortsDeliveryListener {
Connection conn = null;
PreparedStatement preparedStatement = null;
try {
conn = bedSoftTransaction.getConnection().getConnection(event.getDataSource(), SQLType.DML);
conn = bedSoftTransaction.getConnection().getConnection(event.getDataSource());
if (!isValidConnection(conn)) {
bedSoftTransaction.getConnection().release(conn);
conn = bedSoftTransaction.getConnection().getConnection(event.getDataSource(), SQLType.DML);
conn = bedSoftTransaction.getConnection().getConnection(event.getDataSource());
isNewConnection = true;
}
preparedStatement = conn.prepareStatement(event.getSqlUnit().getSql());
......
......@@ -19,7 +19,6 @@ package io.shardingjdbc.transaction.base;
import io.shardingjdbc.core.api.config.ShardingRuleConfiguration;
import io.shardingjdbc.core.api.config.TableRuleConfiguration;
import io.shardingjdbc.core.constant.SQLType;
import io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource;
import io.shardingjdbc.core.rule.ShardingRule;
import lombok.AccessLevel;
......@@ -86,7 +85,7 @@ public abstract class AbstractSoftTransactionIntegrationTest {
+ "`id` int NOT NULL, "
+ "PRIMARY KEY (`id`));";
try (
Connection conn = shardingDataSource.getConnection().getConnection("db_trans", SQLType.DQL);
Connection conn = shardingDataSource.getConnection().getConnection("db_trans");
PreparedStatement preparedStatement = conn.prepareStatement(dbSchema)) {
preparedStatement.executeUpdate();
} catch (final SQLException e) {
......
......@@ -18,7 +18,6 @@
package io.shardingjdbc.transaction.integrate;
import io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource;
import io.shardingjdbc.core.constant.SQLType;
import io.shardingjdbc.transaction.api.SoftTransactionManager;
import io.shardingjdbc.transaction.api.config.SoftTransactionConfiguration;
import io.shardingjdbc.transaction.base.AbstractSoftTransactionIntegrationTest;
......@@ -50,7 +49,7 @@ public final class SoftTransactionTest extends AbstractSoftTransactionIntegratio
private void insert() {
String dbSchema = "insert into transaction_test(id) values (1)";
try (
Connection conn = getShardingDataSource().getConnection().getConnection("db_trans", SQLType.DML);
Connection conn = getShardingDataSource().getConnection().getConnection("db_trans");
PreparedStatement preparedStatement = conn.prepareStatement(dbSchema)) {
preparedStatement.executeUpdate();
} catch (final SQLException e) {
......@@ -62,7 +61,7 @@ public final class SoftTransactionTest extends AbstractSoftTransactionIntegratio
String dbSchema = "select * from `transaction_test`;";
int id = 0;
try (
Connection conn = getShardingDataSource().getConnection().getConnection("db_trans", SQLType.DQL);
Connection conn = getShardingDataSource().getConnection().getConnection("db_trans");
PreparedStatement preparedStatement = conn.prepareStatement(dbSchema)) {
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册