From 2db4e15f0e5fa6a4bdf3bb46d0d280ea16fc8dea Mon Sep 17 00:00:00 2001 From: Chris Beams Date: Tue, 15 May 2012 14:50:31 +0300 Subject: [PATCH] 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 --- .../jdbc/support/SQLExceptionSubclassTranslator.java | 3 ++- .../jdbc/support/SQLExceptionSubclassTranslatorTests.java | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/support/SQLExceptionSubclassTranslator.java b/spring-jdbc/src/main/java/org/springframework/jdbc/support/SQLExceptionSubclassTranslator.java index fa676fddc91..519785aaa79 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/support/SQLExceptionSubclassTranslator.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/support/SQLExceptionSubclassTranslator.java @@ -36,6 +36,7 @@ import org.springframework.dao.DataAccessResourceFailureException; import org.springframework.dao.DataIntegrityViolationException; import org.springframework.dao.InvalidDataAccessApiUsageException; import org.springframework.dao.PermissionDeniedDataAccessException; +import org.springframework.dao.QueryTimeoutException; import org.springframework.dao.RecoverableDataAccessException; import org.springframework.dao.TransientDataAccessResourceException; import org.springframework.jdbc.BadSqlGrammarException; @@ -71,7 +72,7 @@ public class SQLExceptionSubclassTranslator extends AbstractFallbackSQLException return new TransientDataAccessResourceException(buildMessage(task, sql, ex), ex); } if (ex instanceof SQLTimeoutException) { - return new TransientDataAccessResourceException(buildMessage(task, sql, ex), ex); + return new QueryTimeoutException(buildMessage(task, sql, ex), ex); } } else if (ex instanceof SQLNonTransientException) { diff --git a/spring-jdbc/src/test/java/org/springframework/jdbc/support/SQLExceptionSubclassTranslatorTests.java b/spring-jdbc/src/test/java/org/springframework/jdbc/support/SQLExceptionSubclassTranslatorTests.java index 4c63040b537..2a6fc51091d 100644 --- a/spring-jdbc/src/test/java/org/springframework/jdbc/support/SQLExceptionSubclassTranslatorTests.java +++ b/spring-jdbc/src/test/java/org/springframework/jdbc/support/SQLExceptionSubclassTranslatorTests.java @@ -26,6 +26,7 @@ import org.springframework.dao.DataAccessResourceFailureException; import org.springframework.dao.DataIntegrityViolationException; import org.springframework.dao.InvalidDataAccessApiUsageException; import org.springframework.dao.PermissionDeniedDataAccessException; +import org.springframework.dao.QueryTimeoutException; import org.springframework.dao.RecoverableDataAccessException; import org.springframework.dao.TransientDataAccessResourceException; import org.springframework.jdbc.BadSqlGrammarException; @@ -83,7 +84,7 @@ public class SQLExceptionSubclassTranslatorTests extends TestCase { assertEquals(transientConnEx, tdarex.getCause()); SQLException transientConnEx2 = SQLExceptionSubclassFactory.newSQLTimeoutException("", "", 0); - TransientDataAccessResourceException tdarex2 = (TransientDataAccessResourceException) sext.translate("task", "SQL", transientConnEx2); + QueryTimeoutException tdarex2 = (QueryTimeoutException) sext.translate("task", "SQL", transientConnEx2); assertEquals(transientConnEx2, tdarex2.getCause()); SQLException recoverableEx = SQLExceptionSubclassFactory.newSQLRecoverableException("", "", 0);