Skip to content

Commit

Permalink
fix #522
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Jan 31, 2018
1 parent 2330f26 commit 97b8b3d
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,22 @@ public final class ShardingConnection extends AbstractConnectionAdapter {
private final ShardingContext shardingContext;

/**
* Get all database connections via data source name.
* Get database connections via data source name for DDL.
*
* <p>Master-slave connection will return all actual connections</p>
* <p>Non-Master-slave connection will return actual connection</p>
* <p>Master-slave connection will return actual master connections</p>
*
* @param dataSourceName data source name
* @return all database connections via data source name
* @return all database connections via data source name for DDL
* @throws SQLException SQL exception
*/
public Collection<Connection> getAllConnections(final String dataSourceName) throws SQLException {
// TODO Return value is Connection because will support multiple master datasources in future.
public Collection<Connection> getConnectionsForDDL(final String dataSourceName) throws SQLException {
DataSource dataSource = shardingContext.getShardingRule().getDataSourceMap().get(dataSourceName);
Preconditions.checkState(null != dataSource, "Missing the rule of %s in DataSourceRule", dataSourceName);
Map<String, DataSource> dataSources;
if (dataSource instanceof MasterSlaveDataSource) {
dataSources = ((MasterSlaveDataSource) dataSource).getAllDataSources();
dataSources = ((MasterSlaveDataSource) dataSource).getMasterDataSource();
} else {
dataSources = new HashMap<>(1, 1);
dataSources.put(dataSourceName, dataSource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ private Collection<PreparedStatementUnit> route() throws SQLException {

private Collection<PreparedStatement> generatePreparedStatementForDDL(final SQLExecutionUnit sqlExecutionUnit) throws SQLException {
Collection<PreparedStatement> result = new LinkedList<>();
Collection<Connection> connections = getConnection().getAllConnections(sqlExecutionUnit.getDataSource());
Collection<Connection> connections = getConnection().getConnectionsForDDL(sqlExecutionUnit.getDataSource());
for (Connection each : connections) {
result.add(each.prepareStatement(sqlExecutionUnit.getSql(), resultSetType, resultSetConcurrency, resultSetHoldability));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ private StatementExecutor generateExecutor(final String sql) throws SQLException
Collection<Connection> connections;
SQLType sqlType = routeResult.getSqlStatement().getType();
if (SQLType.DDL == sqlType) {
connections = connection.getAllConnections(each.getDataSource());
connections = connection.getConnectionsForDDL(each.getDataSource());
} else {
connections = Collections.singletonList(connection.getConnection(each.getDataSource(), routeResult.getSqlStatement().getType()));
}
Expand Down

0 comments on commit 97b8b3d

Please sign in to comment.