提交 de89878b 编写于 作者: T terrymanu

add master-slave actual data source name mapping for sharding connection

上级 1aae419b
......@@ -22,9 +22,9 @@ import com.dangdang.ddframe.rdb.sharding.hint.HintManagerHolder;
import com.dangdang.ddframe.rdb.sharding.jdbc.adapter.AbstractConnectionAdapter;
import com.dangdang.ddframe.rdb.sharding.jdbc.core.ShardingContext;
import com.dangdang.ddframe.rdb.sharding.jdbc.core.datasource.MasterSlaveDataSource;
import com.dangdang.ddframe.rdb.sharding.jdbc.core.datasource.NamedDataSource;
import com.dangdang.ddframe.rdb.sharding.jdbc.core.statement.ShardingPreparedStatement;
import com.dangdang.ddframe.rdb.sharding.jdbc.core.statement.ShardingStatement;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
......@@ -87,16 +87,19 @@ public final class ShardingConnection extends AbstractConnectionAdapter {
* @throws SQLException SQL exception
*/
public Connection getConnection(final String dataSourceName, final SQLType sqlType) throws SQLException {
Optional<Connection> connection = getCachedConnection(dataSourceName, sqlType);
if (connection.isPresent()) {
return connection.get();
if (cachedConnections.containsKey(dataSourceName)) {
return cachedConnections.get(dataSourceName);
}
DataSource dataSource = shardingContext.getShardingRule().getDataSourceRule().getDataSource(dataSourceName);
Preconditions.checkState(null != dataSource, "Missing the rule of %s in DataSourceRule", dataSourceName);
String realDataSourceName;
if (dataSource instanceof MasterSlaveDataSource) {
dataSource = ((MasterSlaveDataSource) dataSource).getDataSource(sqlType).getDataSource();
realDataSourceName = MasterSlaveDataSource.getDataSourceName(dataSourceName, sqlType);
NamedDataSource namedDataSource = ((MasterSlaveDataSource) dataSource).getDataSource(sqlType);
realDataSourceName = namedDataSource.getName();
if (cachedConnections.containsKey(realDataSourceName)) {
return cachedConnections.get(realDataSourceName);
}
dataSource = namedDataSource.getDataSource();
} else {
realDataSourceName = dataSourceName;
}
......@@ -106,11 +109,6 @@ public final class ShardingConnection extends AbstractConnectionAdapter {
return result;
}
private Optional<Connection> getCachedConnection(final String dataSourceName, final SQLType sqlType) {
String key = cachedConnections.containsKey(dataSourceName) ? dataSourceName : MasterSlaveDataSource.getDataSourceName(dataSourceName, sqlType);
return Optional.fromNullable(cachedConnections.get(key));
}
/**
* Release connection.
*
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册