Skip to content

Commit

Permalink
[CONJ-1102] BatchUpdateException.getUpdateCounts() wrong results
Browse files Browse the repository at this point in the history
  • Loading branch information
rusher committed Oct 26, 2023
1 parent 69735a5 commit 8690386
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,7 @@ public List<Completion> executePipeline(
return results;
} catch (SQLException sqlException) {
if (!closed) {
results.add(null);
// read remaining results
perMsgCounter++;
for (; perMsgCounter < responseMsg[readCounter - 1]; perMsgCounter++) {
Expand Down Expand Up @@ -626,7 +627,7 @@ public List<Completion> executePipeline(
resultSetType,
closeOnCompletion));
} catch (SQLException e) {
// eat
results.add(null);
}
}
}
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/org/mariadb/jdbc/export/ExceptionFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,6 @@ public BatchUpdateException createBatchUpdate(
public BatchUpdateException createBatchUpdate(
List<Completion> res, int length, int[] responseMsg, SQLException sqle) {
int[] updateCounts = new int[length];

int responseIncrement = 0;
for (int i = 0; i < length; i++) {
if (i >= responseMsg.length) {
Arrays.fill(updateCounts, i, length, Statement.EXECUTE_FAILED);
Expand All @@ -204,8 +202,16 @@ public BatchUpdateException createBatchUpdate(
if (MsgResponseNo < 1) {
updateCounts[0] = Statement.EXECUTE_FAILED;
return new BatchUpdateException(updateCounts, sqle);
} else if (MsgResponseNo == 1 && res.size() > i && res.get(i) instanceof OkPacket) {
updateCounts[i] = (int) ((OkPacket) res.get(i)).getAffectedRows();
} else if (MsgResponseNo == 1) {
if (i >= res.size() || res.get(i) == null) {
updateCounts[i] = Statement.EXECUTE_FAILED;
continue;
}
if (res.get(i) instanceof OkPacket) {
updateCounts[i] = (int) ((OkPacket) res.get(i)).getAffectedRows();
continue;
}
updateCounts[i] = Statement.SUCCESS_NO_INFO;
} else {
// unknown.
updateCounts[i] = Statement.SUCCESS_NO_INFO;
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/org/mariadb/jdbc/integration/BatchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,29 @@ public static void after2() throws SQLException {
stmt.execute("DROP TABLE IF EXISTS BatchTest");
}

@Test
public void batchError() throws SQLException {
Statement stmt = sharedConn.createStatement();
stmt.execute("DROP TABLE IF EXISTS t1");
stmt.execute("CREATE TABLE t1(c0 DATE UNIQUE PRIMARY KEY NOT NULL)");

stmt.addBatch("INSERT INTO t1 VALUES ('2006-04-01')");
stmt.addBatch("INSERT INTO t1 VALUES ('2006-04-01')");
stmt.addBatch("INSERT INTO t1 VALUES ('2019-04-11')");
stmt.addBatch("INSERT INTO t1 VALUES ('2006-04-01')");
stmt.addBatch("INSERT INTO t1 VALUES ('2020-04-11')");
try {
stmt.executeBatch();
fail();
} catch (BatchUpdateException e) {
assertTrue(e.getMessage().contains("Duplicate entry"));
assertEquals(5, e.getUpdateCounts().length);
assertArrayEquals(
new int[] {1, java.sql.Statement.EXECUTE_FAILED, 1, java.sql.Statement.EXECUTE_FAILED, 1},
e.getUpdateCounts());
}
}

@Test
public void wrongParameter() throws SQLException {
try (Connection con = createCon("&useServerPrepStmts=false")) {
Expand Down

0 comments on commit 8690386

Please sign in to comment.