Skip to content

Commit 2db4e15

Browse files
committed
Translate SQLTimeoutException to QueryTimeoutException
SPR-7680 added QueryTimeoutException to Spring's DataAccessException hierarchy, but did not integrate it into the SQLExceptionSubclassTranslator; it was added mainly to accomodate users defining their own custom exception translators. However, it does make sense to translate any SQLTimeoutException to this new QueryTimeoutException type, and this commit makes that change. It does represent a slight backward-incompatibility, given that QueryTimeoutException extends TransientDataAccessException, whereas SQLExceptionSubclassTranslator previously returned the more specific TransientDataAccessResourceException for any SQLTimeoutException. It is expected that this incompatibily will be very low-impact, i.e. not affecting many (if any) users. In any case, a major release (Spring 3.2) is the right time to introduce such a change, and the migration path is straightforward: any users depending on catching TransientDataAccessResourceException in the case of query timeouts should update those catch blocks to expect QueryTimeoutException instead. Care should also be taken to ensure correctness of existing catch blocks expecting TransientDataAccessException, as these blocks will now catch QueryTimeoutException as well. Issue: SPR-9376, SPR-7680
1 parent 2ff4372 commit 2db4e15

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

spring-jdbc/src/main/java/org/springframework/jdbc/support/SQLExceptionSubclassTranslator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.springframework.dao.DataIntegrityViolationException;
3737
import org.springframework.dao.InvalidDataAccessApiUsageException;
3838
import org.springframework.dao.PermissionDeniedDataAccessException;
39+
import org.springframework.dao.QueryTimeoutException;
3940
import org.springframework.dao.RecoverableDataAccessException;
4041
import org.springframework.dao.TransientDataAccessResourceException;
4142
import org.springframework.jdbc.BadSqlGrammarException;
@@ -71,7 +72,7 @@ protected DataAccessException doTranslate(String task, String sql, SQLException
7172
return new TransientDataAccessResourceException(buildMessage(task, sql, ex), ex);
7273
}
7374
if (ex instanceof SQLTimeoutException) {
74-
return new TransientDataAccessResourceException(buildMessage(task, sql, ex), ex);
75+
return new QueryTimeoutException(buildMessage(task, sql, ex), ex);
7576
}
7677
}
7778
else if (ex instanceof SQLNonTransientException) {

spring-jdbc/src/test/java/org/springframework/jdbc/support/SQLExceptionSubclassTranslatorTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.dao.DataIntegrityViolationException;
2727
import org.springframework.dao.InvalidDataAccessApiUsageException;
2828
import org.springframework.dao.PermissionDeniedDataAccessException;
29+
import org.springframework.dao.QueryTimeoutException;
2930
import org.springframework.dao.RecoverableDataAccessException;
3031
import org.springframework.dao.TransientDataAccessResourceException;
3132
import org.springframework.jdbc.BadSqlGrammarException;
@@ -83,7 +84,7 @@ public void testErrorCodeTranslation() {
8384
assertEquals(transientConnEx, tdarex.getCause());
8485

8586
SQLException transientConnEx2 = SQLExceptionSubclassFactory.newSQLTimeoutException("", "", 0);
86-
TransientDataAccessResourceException tdarex2 = (TransientDataAccessResourceException) sext.translate("task", "SQL", transientConnEx2);
87+
QueryTimeoutException tdarex2 = (QueryTimeoutException) sext.translate("task", "SQL", transientConnEx2);
8788
assertEquals(transientConnEx2, tdarex2.getCause());
8889

8990
SQLException recoverableEx = SQLExceptionSubclassFactory.newSQLRecoverableException("", "", 0);

0 commit comments

Comments
 (0)