diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md index fb7cc4b7043b70f59e75b730d4df147c30e450b5..36fddbd6b48c51885b7d6e2fe8dba4d3856ceb3c 100644 --- a/RELEASE-NOTES.md +++ b/RELEASE-NOTES.md @@ -4,6 +4,7 @@ 1. [ISSUE #356](https://github.com/dangdangdotcom/sharding-jdbc/issues/356) 在SQL的Where条件中兼容不是分片列的REGEXP操作符 1. [ISSUE #362](https://github.com/dangdangdotcom/sharding-jdbc/issues/362) 读写分离使用PreparedStatement并未调用setParameter方法导致出错 +1. [ISSUE #370](https://github.com/dangdangdotcom/sharding-jdbc/issues/370) 使用原生自增主键调用getGeneratedKeys出错 ## 1.5.3 diff --git a/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/jdbc/core/statement/ShardingPreparedStatement.java b/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/jdbc/core/statement/ShardingPreparedStatement.java index 2aba88601dc8ac57b31114597e7a5a33ee662873..2d93cf1fbcf6a0969f36e839098496950f98c6ff 100644 --- a/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/jdbc/core/statement/ShardingPreparedStatement.java +++ b/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/jdbc/core/statement/ShardingPreparedStatement.java @@ -174,19 +174,9 @@ public final class ShardingPreparedStatement extends AbstractShardingPreparedSta } private PreparedStatement generatePreparedStatement(final SQLExecutionUnit sqlExecutionUnit) throws SQLException { - Optional generatedKey = getGeneratedKey(); Connection connection = getConnection().getConnection(sqlExecutionUnit.getDataSource(), routeResult.getSqlStatement().getType()); - if (returnGeneratedKeys && generatedKey.isPresent()) { - return connection.prepareStatement(sqlExecutionUnit.getSql(), RETURN_GENERATED_KEYS); - } - return connection.prepareStatement(sqlExecutionUnit.getSql(), resultSetType, resultSetConcurrency, resultSetHoldability); - } - - private Optional getGeneratedKey() { - if (null != routeResult && routeResult.getSqlStatement() instanceof InsertStatement) { - return Optional.fromNullable(((InsertStatement) routeResult.getSqlStatement()).getGeneratedKey()); - } - return Optional.absent(); + return returnGeneratedKeys ? connection.prepareStatement(sqlExecutionUnit.getSql(), RETURN_GENERATED_KEYS) + : connection.prepareStatement(sqlExecutionUnit.getSql(), resultSetType, resultSetConcurrency, resultSetHoldability); } @Override @@ -211,28 +201,6 @@ public final class ShardingPreparedStatement extends AbstractShardingPreparedSta } } - @Override - public int[] executeBatch() throws SQLException { - try { - return new BatchPreparedStatementExecutor(getConnection().getShardingContext().getExecutorEngine(), - getConnection().getShardingContext().getDatabaseType(), routeResult.getSqlStatement().getType(), batchStatementUnits, parameterSets).executeBatch(); - } finally { - clearBatch(); - } - } - - @Override - public ResultSet getGeneratedKeys() throws SQLException { - Optional generatedKey = getGeneratedKey(); - if (returnGeneratedKeys && generatedKey.isPresent()) { - return new GeneratedKeysResultSet(routeResult.getGeneratedKeys().iterator(), generatedKey.get().getColumn(), this); - } - if (1 == routedStatements.size()) { - return routedStatements.iterator().next().getGeneratedKeys(); - } - return new GeneratedKeysResultSet(); - } - private List routeBatch() throws SQLException { List result = new ArrayList<>(); routeResult = routingEngine.route(getParameters()); @@ -260,6 +228,35 @@ public final class ShardingPreparedStatement extends AbstractShardingPreparedSta return result; } + @Override + public int[] executeBatch() throws SQLException { + try { + return new BatchPreparedStatementExecutor(getConnection().getShardingContext().getExecutorEngine(), + getConnection().getShardingContext().getDatabaseType(), routeResult.getSqlStatement().getType(), batchStatementUnits, parameterSets).executeBatch(); + } finally { + clearBatch(); + } + } + + @Override + public ResultSet getGeneratedKeys() throws SQLException { + Optional generatedKey = getGeneratedKey(); + if (returnGeneratedKeys && generatedKey.isPresent()) { + return new GeneratedKeysResultSet(routeResult.getGeneratedKeys().iterator(), generatedKey.get().getColumn(), this); + } + if (1 == routedStatements.size()) { + return routedStatements.iterator().next().getGeneratedKeys(); + } + return new GeneratedKeysResultSet(); + } + + private Optional getGeneratedKey() { + if (null != routeResult && routeResult.getSqlStatement() instanceof InsertStatement) { + return Optional.fromNullable(((InsertStatement) routeResult.getSqlStatement()).getGeneratedKey()); + } + return Optional.absent(); + } + @Override public ResultSet getResultSet() throws SQLException { if (null != currentResultSet) {