Skip to content

Commit 595abff

Browse files
unintendedfmbenhassine
authored andcommitted
Fix NPE in sql exception translation
Resolves #3968
1 parent 8d3201c commit 595abff

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

@@ -460,7 +470,7 @@ protected void initializeConnection() {
460470
}
461471
catch (SQLException se) {
462472
close();
463-
throw getExceptionTranslator().translate("Executing query", getSql(), se);
473+
throw translateSqlException("Executing query", getSql(), se);
464474
}
465475
}
466476

@@ -487,7 +497,7 @@ protected T doRead() throws Exception {
487497
return item;
488498
}
489499
catch (SQLException se) {
490-
throw getExceptionTranslator().translate("Attempt to process next row failed", getSql(), se);
500+
throw translateSqlException("Attempt to process next row failed", getSql(), se);
491501
}
492502
}
493503

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
@@ -130,7 +130,7 @@ protected void openCursor(Connection con) {
130130
}
131131
catch (SQLException se) {
132132
close();
133-
throw getExceptionTranslator().translate("Executing query", getSql(), se);
133+
throw translateSqlException("Executing query", getSql(), se);
134134
}
135135

136136
}

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
@@ -227,7 +227,7 @@ protected void openCursor(Connection con) {
227227
}
228228
catch (SQLException se) {
229229
close();
230-
throw getExceptionTranslator().translate("Executing stored procedure", getSql(), se);
230+
throw translateSqlException("Executing stored procedure", getSql(), se);
231231
}
232232

233233
}

0 commit comments

Comments
 (0)