Skip to content

Commit d8f8e76

Browse files
committed
Check potentially more specific HibernateException cause as well
Closes gh-34633
1 parent dc41ff5 commit d8f8e76

File tree

3 files changed

+106
-36
lines changed

3 files changed

+106
-36
lines changed

Diff for: spring-orm/src/main/java/org/springframework/orm/jpa/vendor/HibernateJpaDialect.java

+31-24
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -253,14 +253,18 @@ public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
253253
* @return the corresponding DataAccessException instance
254254
*/
255255
protected DataAccessException convertHibernateAccessException(HibernateException ex) {
256-
if (this.jdbcExceptionTranslator != null && ex instanceof JDBCException jdbcEx) {
256+
return convertHibernateAccessException(ex, ex);
257+
}
258+
259+
private DataAccessException convertHibernateAccessException(HibernateException ex, HibernateException exToCheck) {
260+
if (this.jdbcExceptionTranslator != null && exToCheck instanceof JDBCException jdbcEx) {
257261
DataAccessException dae = this.jdbcExceptionTranslator.translate(
258262
"Hibernate operation: " + jdbcEx.getMessage(), jdbcEx.getSQL(), jdbcEx.getSQLException());
259263
if (dae != null) {
260264
return dae;
261265
}
262266
}
263-
if (this.transactionExceptionTranslator != null && ex instanceof org.hibernate.TransactionException) {
267+
if (this.transactionExceptionTranslator != null && exToCheck instanceof org.hibernate.TransactionException) {
264268
if (ex.getCause() instanceof SQLException sqlEx) {
265269
DataAccessException dae = this.transactionExceptionTranslator.translate(
266270
"Hibernate transaction: " + ex.getMessage(), null, sqlEx);
@@ -270,74 +274,77 @@ protected DataAccessException convertHibernateAccessException(HibernateException
270274
}
271275
}
272276

273-
if (ex instanceof JDBCConnectionException) {
277+
if (exToCheck instanceof JDBCConnectionException) {
274278
return new DataAccessResourceFailureException(ex.getMessage(), ex);
275279
}
276-
if (ex instanceof SQLGrammarException hibEx) {
280+
if (exToCheck instanceof SQLGrammarException hibEx) {
277281
return new InvalidDataAccessResourceUsageException(ex.getMessage() + "; SQL [" + hibEx.getSQL() + "]", ex);
278282
}
279-
if (ex instanceof QueryTimeoutException hibEx) {
283+
if (exToCheck instanceof QueryTimeoutException hibEx) {
280284
return new org.springframework.dao.QueryTimeoutException(ex.getMessage() + "; SQL [" + hibEx.getSQL() + "]", ex);
281285
}
282-
if (ex instanceof LockAcquisitionException hibEx) {
286+
if (exToCheck instanceof LockAcquisitionException hibEx) {
283287
return new CannotAcquireLockException(ex.getMessage() + "; SQL [" + hibEx.getSQL() + "]", ex);
284288
}
285-
if (ex instanceof PessimisticLockException hibEx) {
289+
if (exToCheck instanceof PessimisticLockException hibEx) {
286290
return new PessimisticLockingFailureException(ex.getMessage() + "; SQL [" + hibEx.getSQL() + "]", ex);
287291
}
288-
if (ex instanceof ConstraintViolationException hibEx) {
292+
if (exToCheck instanceof ConstraintViolationException hibEx) {
289293
return new DataIntegrityViolationException(ex.getMessage() + "; SQL [" + hibEx.getSQL() +
290294
"]; constraint [" + hibEx.getConstraintName() + "]", ex);
291295
}
292-
if (ex instanceof DataException hibEx) {
296+
if (exToCheck instanceof DataException hibEx) {
293297
return new DataIntegrityViolationException(ex.getMessage() + "; SQL [" + hibEx.getSQL() + "]", ex);
294298
}
295299
// end of JDBCException subclass handling
296300

297-
if (ex instanceof QueryException) {
301+
if (exToCheck instanceof QueryException) {
298302
return new InvalidDataAccessResourceUsageException(ex.getMessage(), ex);
299303
}
300-
if (ex instanceof NonUniqueResultException) {
304+
if (exToCheck instanceof NonUniqueResultException) {
301305
return new IncorrectResultSizeDataAccessException(ex.getMessage(), 1, ex);
302306
}
303-
if (ex instanceof NonUniqueObjectException) {
307+
if (exToCheck instanceof NonUniqueObjectException) {
304308
return new DuplicateKeyException(ex.getMessage(), ex);
305309
}
306-
if (ex instanceof PropertyValueException) {
310+
if (exToCheck instanceof PropertyValueException) {
307311
return new DataIntegrityViolationException(ex.getMessage(), ex);
308312
}
309-
if (ex instanceof PersistentObjectException) {
313+
if (exToCheck instanceof PersistentObjectException) {
310314
return new InvalidDataAccessApiUsageException(ex.getMessage(), ex);
311315
}
312-
if (ex instanceof TransientObjectException) {
316+
if (exToCheck instanceof TransientObjectException) {
313317
return new InvalidDataAccessApiUsageException(ex.getMessage(), ex);
314318
}
315-
if (ex instanceof ObjectDeletedException) {
319+
if (exToCheck instanceof ObjectDeletedException) {
316320
return new InvalidDataAccessApiUsageException(ex.getMessage(), ex);
317321
}
318-
if (ex instanceof UnresolvableObjectException hibEx) {
322+
if (exToCheck instanceof UnresolvableObjectException hibEx) {
319323
return new ObjectRetrievalFailureException(hibEx.getEntityName(), getIdentifier(hibEx), ex.getMessage(), ex);
320324
}
321-
if (ex instanceof WrongClassException hibEx) {
325+
if (exToCheck instanceof WrongClassException hibEx) {
322326
return new ObjectRetrievalFailureException(hibEx.getEntityName(), getIdentifier(hibEx), ex.getMessage(), ex);
323327
}
324-
if (ex instanceof StaleObjectStateException hibEx) {
328+
if (exToCheck instanceof StaleObjectStateException hibEx) {
325329
return new ObjectOptimisticLockingFailureException(hibEx.getEntityName(), getIdentifier(hibEx), ex.getMessage(), ex);
326330
}
327-
if (ex instanceof StaleStateException) {
331+
if (exToCheck instanceof StaleStateException) {
328332
return new ObjectOptimisticLockingFailureException(ex.getMessage(), ex);
329333
}
330-
if (ex instanceof OptimisticEntityLockException) {
334+
if (exToCheck instanceof OptimisticEntityLockException) {
331335
return new ObjectOptimisticLockingFailureException(ex.getMessage(), ex);
332336
}
333-
if (ex instanceof PessimisticEntityLockException) {
337+
if (exToCheck instanceof PessimisticEntityLockException) {
334338
if (ex.getCause() instanceof LockAcquisitionException) {
335339
return new CannotAcquireLockException(ex.getMessage(), ex.getCause());
336340
}
337341
return new PessimisticLockingFailureException(ex.getMessage(), ex);
338342
}
339343

340-
// fallback
344+
// Fallback: check potentially more specific cause, otherwise JpaSystemException
345+
if (exToCheck.getCause() instanceof HibernateException causeToCheck) {
346+
return convertHibernateAccessException(ex, causeToCheck);
347+
}
341348
return new JpaSystemException(ex);
342349
}
343350

Diff for: spring-orm/src/test/java/org/springframework/orm/jpa/DefaultJpaDialectTests.java

+17-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
import jakarta.persistence.EntityManager;
2020
import jakarta.persistence.EntityTransaction;
2121
import jakarta.persistence.OptimisticLockException;
22+
import jakarta.persistence.PersistenceException;
2223
import org.junit.jupiter.api.Test;
2324

2425
import org.springframework.transaction.TransactionDefinition;
@@ -33,33 +34,37 @@
3334
/**
3435
* @author Costin Leau
3536
* @author Phillip Webb
37+
* @author Juergen Hoeller
3638
*/
3739
class DefaultJpaDialectTests {
3840

39-
private JpaDialect dialect = new DefaultJpaDialect();
41+
private final JpaDialect dialect = new DefaultJpaDialect();
4042

41-
@Test
42-
void testDefaultTransactionDefinition() {
43-
DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
44-
definition.setIsolationLevel(TransactionDefinition.ISOLATION_REPEATABLE_READ);
45-
assertThatExceptionOfType(TransactionException.class).isThrownBy(() ->
46-
dialect.beginTransaction(null, definition));
47-
}
4843

4944
@Test
5045
void testDefaultBeginTransaction() throws Exception {
5146
TransactionDefinition definition = new DefaultTransactionDefinition();
5247
EntityManager entityManager = mock();
5348
EntityTransaction entityTx = mock();
54-
5549
given(entityManager.getTransaction()).willReturn(entityTx);
5650

5751
dialect.beginTransaction(entityManager, definition);
5852
}
5953

54+
@Test
55+
void testCustomIsolationLevel() {
56+
DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
57+
definition.setIsolationLevel(TransactionDefinition.ISOLATION_REPEATABLE_READ);
58+
59+
assertThatExceptionOfType(TransactionException.class).isThrownBy(() ->
60+
dialect.beginTransaction(null, definition));
61+
}
62+
6063
@Test
6164
void testTranslateException() {
62-
OptimisticLockException ex = new OptimisticLockException();
63-
assertThat(dialect.translateExceptionIfPossible(ex).getCause()).isEqualTo(EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(ex).getCause());
65+
PersistenceException ex = new OptimisticLockException();
66+
assertThat(dialect.translateExceptionIfPossible(ex))
67+
.isInstanceOf(JpaOptimisticLockingFailureException.class).hasCause(ex);
6468
}
69+
6570
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2002-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.orm.jpa.hibernate;
18+
19+
import jakarta.persistence.OptimisticLockException;
20+
import jakarta.persistence.PersistenceException;
21+
import org.hibernate.HibernateException;
22+
import org.hibernate.dialect.lock.OptimisticEntityLockException;
23+
import org.junit.jupiter.api.Test;
24+
25+
import org.springframework.orm.ObjectOptimisticLockingFailureException;
26+
import org.springframework.orm.jpa.JpaDialect;
27+
import org.springframework.orm.jpa.JpaOptimisticLockingFailureException;
28+
import org.springframework.orm.jpa.vendor.HibernateJpaDialect;
29+
30+
import static org.assertj.core.api.Assertions.assertThat;
31+
32+
/**
33+
* @author Juergen Hoeller
34+
*/
35+
class HibernateJpaDialectTests {
36+
37+
private final JpaDialect dialect = new HibernateJpaDialect();
38+
39+
40+
@Test
41+
void testTranslateException() {
42+
// Plain JPA exception
43+
PersistenceException ex = new OptimisticLockException();
44+
assertThat(dialect.translateExceptionIfPossible(ex))
45+
.isInstanceOf(JpaOptimisticLockingFailureException.class).hasCause(ex);
46+
47+
// Hibernate-specific exception
48+
ex = new OptimisticEntityLockException("", "");
49+
assertThat(dialect.translateExceptionIfPossible(ex))
50+
.isInstanceOf(ObjectOptimisticLockingFailureException.class).hasCause(ex);
51+
52+
// Nested Hibernate-specific exception
53+
ex = new HibernateException(new OptimisticEntityLockException("", ""));
54+
assertThat(dialect.translateExceptionIfPossible(ex))
55+
.isInstanceOf(ObjectOptimisticLockingFailureException.class).hasCause(ex);
56+
}
57+
58+
}

0 commit comments

Comments
 (0)