Skip to content

Commit 91b393c

Browse files
unintendedfmbenhassine
authored andcommitted
Fix NPE in sql exception translation
Resolves #3968
1 parent 78d82a5 commit 91b393c

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/AbstractCursorItemReader.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@
3232
import org.springframework.batch.item.ReaderNotOpenException;
3333
import org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader;
3434
import org.springframework.beans.factory.InitializingBean;
35+
import org.springframework.dao.DataAccessException;
3536
import org.springframework.dao.InvalidDataAccessApiUsageException;
3637
import org.springframework.dao.InvalidDataAccessResourceUsageException;
3738
import org.springframework.jdbc.SQLWarningException;
39+
import org.springframework.jdbc.UncategorizedSQLException;
3840
import org.springframework.jdbc.datasource.DataSourceUtils;
3941
import org.springframework.jdbc.support.JdbcUtils;
4042
import org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator;
@@ -220,6 +222,14 @@ protected SQLExceptionTranslator getExceptionTranslator() {
220222
return exceptionTranslator;
221223
}
222224

225+
protected DataAccessException translateSqlException(String task, String sql, SQLException ex) {
226+
DataAccessException dae = getExceptionTranslator().translate(task, sql, ex);
227+
if (dae != null) {
228+
return dae;
229+
}
230+
return new UncategorizedSQLException(task, sql, ex);
231+
}
232+
223233
/**
224234
* Throw a SQLWarningException if we're not ignoring warnings, else log the
225235
* warnings (at debug level).
@@ -262,7 +272,7 @@ private void moveCursorToRow(int row) {
262272
}
263273
}
264274
catch (SQLException se) {
265-
throw getExceptionTranslator().translate("Attempted to move ResultSet to last committed row", getSql(), se);
275+
throw translateSqlException("Attempted to move ResultSet to last committed row", getSql(), se);
266276
}
267277
}
268278

@@ -455,7 +465,7 @@ protected void initializeConnection() {
455465
}
456466
catch (SQLException se) {
457467
close();
458-
throw getExceptionTranslator().translate("Executing query", getSql(), se);
468+
throw translateSqlException("Executing query", getSql(), se);
459469
}
460470
}
461471

@@ -482,7 +492,7 @@ protected T doRead() throws Exception {
482492
return item;
483493
}
484494
catch (SQLException se) {
485-
throw getExceptionTranslator().translate("Attempt to process next row failed", getSql(), se);
495+
throw translateSqlException("Attempt to process next row failed", getSql(), se);
486496
}
487497
}
488498

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/JdbcCursorItemReader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ protected void openCursor(Connection con) {
129129
}
130130
catch (SQLException se) {
131131
close();
132-
throw getExceptionTranslator().translate("Executing query", getSql(), se);
132+
throw translateSqlException("Executing query", getSql(), se);
133133
}
134134

135135
}

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/StoredProcedureItemReader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ protected void openCursor(Connection con) {
226226
}
227227
catch (SQLException se) {
228228
close();
229-
throw getExceptionTranslator().translate("Executing stored procedure", getSql(), se);
229+
throw translateSqlException("Executing stored procedure", getSql(), se);
230230
}
231231

232232
}

0 commit comments

Comments
 (0)