Skip to content

Commit

Permalink
Include specific SQL statements in batch exception
Browse files Browse the repository at this point in the history
Refine the SQL statements contained in exceptions thrown from batch
updates based on BatchUpdateException.getUpdateCounts().

Issue: SPR-10677
  • Loading branch information
philwebb committed Jun 26, 2013
1 parent 5ee6bb9 commit 6a3a361
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.BatchUpdateException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
Expand Down Expand Up @@ -564,11 +565,24 @@ public int[] doInStatement(Statement stmt) throws SQLException, DataAccessExcept

if (JdbcUtils.supportsBatchUpdates(stmt.getConnection())) {
for (String sqlStmt : sql) {
this.currSql = (StringUtils.isEmpty(this.currSql) ? sqlStmt
: this.currSql + "; " + sqlStmt);
this.currSql = appendSql(this.currSql, sqlStmt);
stmt.addBatch(sqlStmt);
}
rowsAffected = stmt.executeBatch();
try {
rowsAffected = stmt.executeBatch();
}
catch (BatchUpdateException ex) {
String batchExceptionSql = null;
for (int i = 0; i < ex.getUpdateCounts().length; i++) {
if (ex.getUpdateCounts()[i] == Statement.EXECUTE_FAILED) {
batchExceptionSql = appendSql(batchExceptionSql, sql[i]);
}
}
if (StringUtils.hasLength(batchExceptionSql)) {
this.currSql = batchExceptionSql;
}
throw ex;
}
}
else {
for (int i = 0; i < sql.length; i++) {
Expand All @@ -583,6 +597,11 @@ public int[] doInStatement(Statement stmt) throws SQLException, DataAccessExcept
}
return rowsAffected;
}

private String appendSql(String sql, String statement) {
return (StringUtils.isEmpty(sql) ? statement : sql + "; " + statement);
}

@Override
public String getSql() {
return this.currSql;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.jdbc.core;

import java.sql.BatchUpdateException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
Expand Down Expand Up @@ -458,6 +459,24 @@ public void testBatchUpdate() throws Exception {
verify(this.connection, atLeastOnce()).close();
}

@Test
public void testBatchUpdateWithBatchFailure() throws Exception {
final String[] sql = {"A", "B", "C", "D"};
given(this.statement.executeBatch()).willThrow(
new BatchUpdateException(new int[] { 1, Statement.EXECUTE_FAILED, 1,
Statement.EXECUTE_FAILED }));
mockDatabaseMetaData(true);
given(this.connection.createStatement()).willReturn(this.statement);

JdbcTemplate template = new JdbcTemplate(this.dataSource, false);
try {
template.batchUpdate(sql);
}
catch (UncategorizedSQLException ex) {
assertThat(ex.getSql(), equalTo("B; D"));
}
}

@Test
public void testBatchUpdateWithNoBatchSupport() throws Exception {
final String[] sql = {"UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = 1",
Expand Down

0 comments on commit 6a3a361

Please sign in to comment.