Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[CONJ-839] batch exception message correction when with option `rewri…
…teBatchedStatements`

batch message was resulting in Error reading results <number> in place of error.
  • Loading branch information
rusher committed Nov 17, 2020
1 parent 29df5f5 commit 70bdf81
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
Expand Up @@ -671,18 +671,30 @@ public SQLException handleResultException(
int counter = results.getCurrentStatNumber() - 1;
ParameterHolder[] parameters = parametersList.get(counter);
List<byte[]> queryParts = clientPrepareResult.getQueryParts();
StringBuilder sql = new StringBuilder(new String(queryParts.get(0)));

for (int i = 0; i < paramCount; i++) {
sql.append(parameters[i].toString()).append(new String(queryParts.get(i + 1)));
StringBuilder sql;
if (clientPrepareResult.isRewriteType()) {
// build sql from rewrite parsing
sql = new StringBuilder(new String(queryParts.get(0)));
sql.append(new String(queryParts.get(1)));
for (int i = 0; i < paramCount; i++) {
sql.append(parameters[i].toString()).append(new String(queryParts.get(i + 2)));
}
sql.append(new String(queryParts.get(paramCount + 2)));
} else {
// build sql from basic parsing
sql = new StringBuilder(new String(queryParts.get(0)));
for (int i = 0; i < paramCount; i++) {
sql.append(parameters[i].toString()).append(new String(queryParts.get(i + 1)));
}
}

return exceptionWithQuery(sql.toString(), qex, explicitClosed);
}

@Override
public int getParamCount() {
return clientPrepareResult.getQueryParts().size() - 1;
return clientPrepareResult.getParamCount();
}

@Override
Expand Down
Expand Up @@ -398,6 +398,38 @@ public void rewriteBatchedError() throws Exception {
}
}

@Test
public void rewriteErrorException() throws Exception {
try (Connection connection =
setConnection("&rewriteBatchedStatements=true&dumpQueriesOnException")) {
ensureErrorException(connection);
}
try (Connection connection =
setConnection("&rewriteBatchedStatements=false&dumpQueriesOnException")) {
ensureErrorException(connection);
}
}

private void ensureErrorException(Connection connection) throws SQLException {
PreparedStatement pstmt =
connection.prepareStatement("UPDATE unknownTable SET col1 = ?, col2 = 0 WHERE col3 = ?;");
pstmt.setInt(1, 10);
pstmt.setInt(2, 20);
pstmt.addBatch();
pstmt.setInt(1, 100);
pstmt.setInt(2, 200);
try {
pstmt.executeBatch();
fail("Must have thrown error");
} catch (SQLException sqle) {
assertTrue(sqle.getMessage(), sqle.getMessage().contains("doesn't exist"));
assertTrue(
sqle.getMessage(),
sqle.getMessage()
.contains("Query is: UPDATE unknownTable SET col1 = 10, col2 = 0 WHERE col3 = 20;"));
}
}

@Test
public void testLastInsertId() throws Exception {
assertTrue(
Expand Down

0 comments on commit 70bdf81

Please sign in to comment.