分表+读写分离的时候 ddl语句和 @522 bug 一样会获取从库链接
//ShardingStatement类,(ShardingPreparedStatement类也有ddl获取全部链接的代码)
private StatementExecutor generateExecutor(final String sql) throws SQLException {
clearPrevious();
routeResult = new StatementRoutingEngine(connection.getShardingContext()).route(sql);
Collection<StatementUnit> statementUnits = new LinkedList<>();
for (SQLExecutionUnit each : routeResult.getExecutionUnits()) {
Collection<Connection> connections;
SQLType sqlType = routeResult.getSqlStatement().getType();
if (SQLType.DDL == sqlType) {
connections = connection.getAllConnections(each.getDataSource());
} else {
connections = Collections.singletonList(connection.getConnection(each.getDataSource(), routeResult.getSqlStatement().getType()));
}
for (Connection connection : connections) {
Statement statement = connection.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
replayMethodsInvocation(statement);
statementUnits.add(new StatementUnit(each, statement));
routedStatements.add(statement);
}
}
return new StatementExecutor(connection.getShardingContext().getExecutorEngine(), routeResult.getSqlStatement().getType(), statementUnits);
}
//ShardingConnection 类
public Collection
//MasterSlaveDataSource 类 public Map<String, DataSource> getAllDataSources() { Map<String, DataSource> result = new HashMap<>(masterSlaveRule.getSlaveDataSourceMap().size() + 1, 1); result.put(masterSlaveRule.getMasterDataSourceName(), masterSlaveRule.getMasterDataSource()); result.putAll(masterSlaveRule.getSlaveDataSourceMap()); return result; }
您并未提供版本号。这个bug已经修复,应该是使用的版本比较旧导致
最新的2.0.2版本还是获取全部链接, 我是配置的读写分离+分表,发现从库复制失败了,查看日志是ddl语句的问题,最后看源码才发现问题,
ShardingConnection 类这里有判断是读写分离的获取全部数据源,数据源 Map<String, DataSource> dataSources; if (dataSource instanceof MasterSlaveDataSource) { dataSources = ((MasterSlaveDataSource) dataSource).getAllDataSources(); } else { dataSources = new HashMap<>(1, 1); dataSources.put(dataSourceName, dataSource); }
// 循环数据源,返回链接集合 for (Entry<String, DataSource> entry : dataSources.entrySet()) { Connection connection = getCachedConnections().containsKey(entry.getKey()) ? getCachedConnections().get(entry.getKey()) : entry.getValue().getConnection(); replayMethodsInvocation(connection); getCachedConnections().put(entry.getKey(), connection); result.add(connection); }
是的,这个问题貌似并没有修复,最新版本同样存在