Skip to content

Commit c90aa63

Browse files
unintendedfmbenhassine
authored andcommitted
Fix NPE in sql exception translation
Resolves #3968
1 parent a318adb commit c90aa63

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

@@ -470,7 +480,7 @@ protected void initializeConnection() {
470480
}
471481
catch (SQLException se) {
472482
close();
473-
throw getExceptionTranslator().translate("Executing query", getSql(), se);
483+
throw translateSqlException("Executing query", getSql(), se);
474484
}
475485
}
476486

@@ -497,7 +507,7 @@ protected T doRead() throws Exception {
497507
return item;
498508
}
499509
catch (SQLException se) {
500-
throw getExceptionTranslator().translate("Attempt to process next row failed", getSql(), se);
510+
throw translateSqlException("Attempt to process next row failed", getSql(), se);
501511
}
502512
}
503513

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)