fix(jdbc): fall back when statement batches are unsupported

Closes #5556
This commit is contained in:
t8y2 committed 2026-08-07 01:51:22 +08:00
1 parent cda6fd2600
commit 4402faac1c
2 files changed
+109 -19

No files matched your search

@@ -32,25 +32,8 @@ public final class BatchExecutor {
return unchecked(() -> {
long start = System.currentTimeMillis();
applySchema(conn, schema, setSchemaSql, resetSchemaSql);
long totalAffected = 0;
int statementCount = 0;
try (Statement stmt = conn.createStatement()) {
for (String statement : statements) {
String trimmed = JdbcExecutor.trimSql(statement);
if (trimmed.isEmpty()) {
continue;
}
stmt.addBatch(trimmed);
statementCount++;
}
if (statementCount > 0) {
totalAffected = affectedRows(executeBatch(stmt));
}
} catch (BatchUpdateException e) {
long[] counts = e.getLargeUpdateCounts();
int failedIndex = counts == null ? 1 : counts.length + 1;
throw new RuntimeException("Statement " + failedIndex + " failed: " + e.getMessage(), e);
}
Long batchAffected = tryExecuteBatch(conn, statements);
long totalAffected = batchAffected == null ? executeIndividually(conn, statements) : batchAffected;
return new QueryResult(
Collections.emptyList(),
Collections.emptyList(),
@@ -61,6 +44,54 @@ public final class BatchExecutor {
});
}
private static Long tryExecuteBatch(Connection conn, List<String> statements) throws Exception {
try (Statement stmt = conn.createStatement()) {
try {
int statementCount = 0;
for (String statement : statements) {
String trimmed = JdbcExecutor.trimSql(statement);
if (trimmed.isEmpty()) {
continue;
}
stmt.addBatch(trimmed);
statementCount++;
}
return statementCount == 0 ? 0L : affectedRows(executeBatch(stmt));
} catch (BatchUpdateException e) {
long[] counts = e.getLargeUpdateCounts();
int failedIndex = counts == null ? 1 : counts.length + 1;
throw new RuntimeException("Statement " + failedIndex + " failed: " + e.getMessage(), e);
} catch (SQLFeatureNotSupportedException | UnsupportedOperationException | AbstractMethodError e) {
return null;
}
}
}
private static long executeIndividually(Connection conn, List<String> statements) throws Exception {
long totalAffected = 0;
int statementIndex = 0;
try (Statement stmt = conn.createStatement()) {
for (String statement : statements) {
String trimmed = JdbcExecutor.trimSql(statement);
if (trimmed.isEmpty()) {
continue;
}
statementIndex++;
try {
if (!stmt.execute(trimmed)) {
long updateCount = updateCount(stmt);
if (updateCount >= 0) {
totalAffected += updateCount;
}
}
} catch (Exception e) {
throw new RuntimeException("Statement " + statementIndex + " failed: " + e.getMessage(), e);
}
}
}
return totalAffected;
}
private static long affectedRows(long[] updateCounts) {
long total = 0;
if (updateCounts == null) {
@@ -89,6 +120,14 @@ public final class BatchExecutor {
}
}
private static long updateCount(Statement stmt) throws Exception {
try {
return stmt.getLargeUpdateCount();
} catch (SQLFeatureNotSupportedException | UnsupportedOperationException | AbstractMethodError e) {
return stmt.getUpdateCount();
}
}
private static void applySchema(
Connection conn,
String schema,