Skip to content

Commit 8690386

Browse files
committed
[CONJ-1102] BatchUpdateException.getUpdateCounts() wrong results
1 parent 69735a5 commit 8690386

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

src/main/java/org/mariadb/jdbc/client/impl/StandardClient.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,7 @@ public List<Completion> executePipeline(
595595
return results;
596596
} catch (SQLException sqlException) {
597597
if (!closed) {
598+
results.add(null);
598599
// read remaining results
599600
perMsgCounter++;
600601
for (; perMsgCounter < responseMsg[readCounter - 1]; perMsgCounter++) {
@@ -626,7 +627,7 @@ public List<Completion> executePipeline(
626627
resultSetType,
627628
closeOnCompletion));
628629
} catch (SQLException e) {
629-
// eat
630+
results.add(null);
630631
}
631632
}
632633
}

src/main/java/org/mariadb/jdbc/export/ExceptionFactory.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,6 @@ public BatchUpdateException createBatchUpdate(
193193
public BatchUpdateException createBatchUpdate(
194194
List<Completion> res, int length, int[] responseMsg, SQLException sqle) {
195195
int[] updateCounts = new int[length];
196-
197-
int responseIncrement = 0;
198196
for (int i = 0; i < length; i++) {
199197
if (i >= responseMsg.length) {
200198
Arrays.fill(updateCounts, i, length, Statement.EXECUTE_FAILED);
@@ -204,8 +202,16 @@ public BatchUpdateException createBatchUpdate(
204202
if (MsgResponseNo < 1) {
205203
updateCounts[0] = Statement.EXECUTE_FAILED;
206204
return new BatchUpdateException(updateCounts, sqle);
207-
} else if (MsgResponseNo == 1 && res.size() > i && res.get(i) instanceof OkPacket) {
208-
updateCounts[i] = (int) ((OkPacket) res.get(i)).getAffectedRows();
205+
} else if (MsgResponseNo == 1) {
206+
if (i >= res.size() || res.get(i) == null) {
207+
updateCounts[i] = Statement.EXECUTE_FAILED;
208+
continue;
209+
}
210+
if (res.get(i) instanceof OkPacket) {
211+
updateCounts[i] = (int) ((OkPacket) res.get(i)).getAffectedRows();
212+
continue;
213+
}
214+
updateCounts[i] = Statement.SUCCESS_NO_INFO;
209215
} else {
210216
// unknown.
211217
updateCounts[i] = Statement.SUCCESS_NO_INFO;

src/test/java/org/mariadb/jdbc/integration/BatchTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,29 @@ public static void after2() throws SQLException {
3131
stmt.execute("DROP TABLE IF EXISTS BatchTest");
3232
}
3333

34+
@Test
35+
public void batchError() throws SQLException {
36+
Statement stmt = sharedConn.createStatement();
37+
stmt.execute("DROP TABLE IF EXISTS t1");
38+
stmt.execute("CREATE TABLE t1(c0 DATE UNIQUE PRIMARY KEY NOT NULL)");
39+
40+
stmt.addBatch("INSERT INTO t1 VALUES ('2006-04-01')");
41+
stmt.addBatch("INSERT INTO t1 VALUES ('2006-04-01')");
42+
stmt.addBatch("INSERT INTO t1 VALUES ('2019-04-11')");
43+
stmt.addBatch("INSERT INTO t1 VALUES ('2006-04-01')");
44+
stmt.addBatch("INSERT INTO t1 VALUES ('2020-04-11')");
45+
try {
46+
stmt.executeBatch();
47+
fail();
48+
} catch (BatchUpdateException e) {
49+
assertTrue(e.getMessage().contains("Duplicate entry"));
50+
assertEquals(5, e.getUpdateCounts().length);
51+
assertArrayEquals(
52+
new int[] {1, java.sql.Statement.EXECUTE_FAILED, 1, java.sql.Statement.EXECUTE_FAILED, 1},
53+
e.getUpdateCounts());
54+
}
55+
}
56+
3457
@Test
3558
public void wrongParameter() throws SQLException {
3659
try (Connection con = createCon("&useServerPrepStmts=false")) {

0 commit comments

Comments
 (0)