shardingsphere icon indicating copy to clipboard operation
shardingsphere copied to clipboard

分表+读写分离的时候 ddl语句和 @522 bug 一样会获取从库链接

Open cwpgh opened this issue 8 years ago • 3 comments

//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 getAllConnections(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(); } else { dataSources = new HashMap<>(1, 1); dataSources.put(dataSourceName, dataSource); } Collection result = new LinkedList<>(); 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); } return result; }

//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; }

cwpgh avatar Jan 22 '18 07:01 cwpgh

您并未提供版本号。这个bug已经修复,应该是使用的版本比较旧导致

terrymanu avatar Jan 22 '18 13:01 terrymanu

最新的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); }

cwpgh avatar Jan 23 '18 01:01 cwpgh

是的,这个问题貌似并没有修复,最新版本同样存在

nobody2014100 avatar Jan 27 '18 05:01 nobody2014100