Skip to content

Commit fdeac9b

Browse files
committed
fix #370
1 parent 830e710 commit fdeac9b

File tree

2 files changed

+32
-34
lines changed

2 files changed

+32
-34
lines changed

RELEASE-NOTES.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
1. [ISSUE #356](https://github.com/dangdangdotcom/sharding-jdbc/issues/356) 在SQL的Where条件中兼容不是分片列的REGEXP操作符
66
1. [ISSUE #362](https://github.com/dangdangdotcom/sharding-jdbc/issues/362) 读写分离使用PreparedStatement并未调用setParameter方法导致出错
7+
1. [ISSUE #370](https://github.com/dangdangdotcom/sharding-jdbc/issues/370) 使用原生自增主键调用getGeneratedKeys出错
78

89
## 1.5.3
910

sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/jdbc/core/statement/ShardingPreparedStatement.java

+31-34
Original file line numberDiff line numberDiff line change
@@ -174,19 +174,9 @@ private Collection<PreparedStatement> generatePreparedStatementForDDL(final SQLE
174174
}
175175

176176
private PreparedStatement generatePreparedStatement(final SQLExecutionUnit sqlExecutionUnit) throws SQLException {
177-
Optional<GeneratedKey> generatedKey = getGeneratedKey();
178177
Connection connection = getConnection().getConnection(sqlExecutionUnit.getDataSource(), routeResult.getSqlStatement().getType());
179-
if (returnGeneratedKeys && generatedKey.isPresent()) {
180-
return connection.prepareStatement(sqlExecutionUnit.getSql(), RETURN_GENERATED_KEYS);
181-
}
182-
return connection.prepareStatement(sqlExecutionUnit.getSql(), resultSetType, resultSetConcurrency, resultSetHoldability);
183-
}
184-
185-
private Optional<GeneratedKey> getGeneratedKey() {
186-
if (null != routeResult && routeResult.getSqlStatement() instanceof InsertStatement) {
187-
return Optional.fromNullable(((InsertStatement) routeResult.getSqlStatement()).getGeneratedKey());
188-
}
189-
return Optional.absent();
178+
return returnGeneratedKeys ? connection.prepareStatement(sqlExecutionUnit.getSql(), RETURN_GENERATED_KEYS)
179+
: connection.prepareStatement(sqlExecutionUnit.getSql(), resultSetType, resultSetConcurrency, resultSetHoldability);
190180
}
191181

192182
@Override
@@ -211,28 +201,6 @@ public void addBatch() throws SQLException {
211201
}
212202
}
213203

214-
@Override
215-
public int[] executeBatch() throws SQLException {
216-
try {
217-
return new BatchPreparedStatementExecutor(getConnection().getShardingContext().getExecutorEngine(),
218-
getConnection().getShardingContext().getDatabaseType(), routeResult.getSqlStatement().getType(), batchStatementUnits, parameterSets).executeBatch();
219-
} finally {
220-
clearBatch();
221-
}
222-
}
223-
224-
@Override
225-
public ResultSet getGeneratedKeys() throws SQLException {
226-
Optional<GeneratedKey> generatedKey = getGeneratedKey();
227-
if (returnGeneratedKeys && generatedKey.isPresent()) {
228-
return new GeneratedKeysResultSet(routeResult.getGeneratedKeys().iterator(), generatedKey.get().getColumn(), this);
229-
}
230-
if (1 == routedStatements.size()) {
231-
return routedStatements.iterator().next().getGeneratedKeys();
232-
}
233-
return new GeneratedKeysResultSet();
234-
}
235-
236204
private List<BatchPreparedStatementUnit> routeBatch() throws SQLException {
237205
List<BatchPreparedStatementUnit> result = new ArrayList<>();
238206
routeResult = routingEngine.route(getParameters());
@@ -260,6 +228,35 @@ public boolean apply(final BatchPreparedStatementUnit input) {
260228
return result;
261229
}
262230

231+
@Override
232+
public int[] executeBatch() throws SQLException {
233+
try {
234+
return new BatchPreparedStatementExecutor(getConnection().getShardingContext().getExecutorEngine(),
235+
getConnection().getShardingContext().getDatabaseType(), routeResult.getSqlStatement().getType(), batchStatementUnits, parameterSets).executeBatch();
236+
} finally {
237+
clearBatch();
238+
}
239+
}
240+
241+
@Override
242+
public ResultSet getGeneratedKeys() throws SQLException {
243+
Optional<GeneratedKey> generatedKey = getGeneratedKey();
244+
if (returnGeneratedKeys && generatedKey.isPresent()) {
245+
return new GeneratedKeysResultSet(routeResult.getGeneratedKeys().iterator(), generatedKey.get().getColumn(), this);
246+
}
247+
if (1 == routedStatements.size()) {
248+
return routedStatements.iterator().next().getGeneratedKeys();
249+
}
250+
return new GeneratedKeysResultSet();
251+
}
252+
253+
private Optional<GeneratedKey> getGeneratedKey() {
254+
if (null != routeResult && routeResult.getSqlStatement() instanceof InsertStatement) {
255+
return Optional.fromNullable(((InsertStatement) routeResult.getSqlStatement()).getGeneratedKey());
256+
}
257+
return Optional.absent();
258+
}
259+
263260
@Override
264261
public ResultSet getResultSet() throws SQLException {
265262
if (null != currentResultSet) {

0 commit comments

Comments
 (0)