Skip to content

Commit 4f5ae22

Browse files
committed
Consistently return empty array in case of empty batch arguments
Issue: SPR-17476 (cherry picked from commit 362c59c)
1 parent 8f1f36d commit 4f5ae22

File tree

4 files changed

+191
-297
lines changed

4 files changed

+191
-297
lines changed

spring-jdbc/src/main/java/org/springframework/jdbc/core/BatchUpdateUtils.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,24 +25,29 @@
2525
* Mainly for internal use within the framework.
2626
*
2727
* @author Thomas Risberg
28+
* @author Juergen Hoeller
2829
* @since 3.0
2930
*/
3031
public abstract class BatchUpdateUtils {
3132

3233
public static int[] executeBatchUpdate(
33-
String sql, final List<Object[]> batchValues, final int[] columnTypes, JdbcOperations jdbcOperations) {
34+
String sql, final List<Object[]> batchArgs, final int[] columnTypes, JdbcOperations jdbcOperations) {
35+
36+
if (batchArgs.isEmpty()) {
37+
return new int[0];
38+
}
3439

3540
return jdbcOperations.batchUpdate(
3641
sql,
3742
new BatchPreparedStatementSetter() {
3843
@Override
3944
public void setValues(PreparedStatement ps, int i) throws SQLException {
40-
Object[] values = batchValues.get(i);
45+
Object[] values = batchArgs.get(i);
4146
setStatementParameters(values, ps, columnTypes);
4247
}
4348
@Override
4449
public int getBatchSize() {
45-
return batchValues.size();
50+
return batchArgs.size();
4651
}
4752
});
4853
}

spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterBatchUpdateUtils.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,15 +28,16 @@
2828
* Mainly for internal use within the framework.
2929
*
3030
* @author Thomas Risberg
31+
* @author Juergen Hoeller
3132
* @since 3.0
3233
*/
3334
public class NamedParameterBatchUpdateUtils extends BatchUpdateUtils {
3435

35-
public static int[] executeBatchUpdateWithNamedParameters(final ParsedSql parsedSql,
36-
final SqlParameterSource[] batchArgs, JdbcOperations jdbcOperations) {
36+
public static int[] executeBatchUpdateWithNamedParameters(
37+
final ParsedSql parsedSql, final SqlParameterSource[] batchArgs, JdbcOperations jdbcOperations) {
3738

38-
if (batchArgs.length <= 0) {
39-
return new int[] {0};
39+
if (batchArgs.length == 0) {
40+
return new int[0];
4041
}
4142

4243
String sqlToUse = NamedParameterUtils.substituteNamedParameters(parsedSql, batchArgs[0]);

0 commit comments

Comments
 (0)