From c762b83a76819bee3b7be368f253021dbbf4c26a Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 20 Aug 2013 19:06:24 +0200 Subject: [PATCH] Explicit support for Hibernate 4.0's QueryTimeoutException, PessimisticLockException, PessimisticEntityLockException and OptimisticEntityLockException Issue: SPR-10815 --- ...nateOptimisticLockingFailureException.java | 10 +++++-- .../orm/hibernate4/SessionFactoryUtils.java | 26 +++++++++++++++++-- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/HibernateOptimisticLockingFailureException.java b/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/HibernateOptimisticLockingFailureException.java index a58b98fda3d..4447aa96449 100644 --- a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/HibernateOptimisticLockingFailureException.java +++ b/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/HibernateOptimisticLockingFailureException.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,12 +18,14 @@ package org.springframework.orm.hibernate4; import org.hibernate.StaleObjectStateException; import org.hibernate.StaleStateException; +import org.hibernate.dialect.lock.OptimisticEntityLockException; import org.springframework.orm.ObjectOptimisticLockingFailureException; /** * Hibernate-specific subclass of ObjectOptimisticLockingFailureException. - * Converts Hibernate's StaleObjectStateException and StaleStateException. + * Converts Hibernate's StaleObjectStateException, StaleStateException + * and OptimisticEntityLockException. * * @author Juergen Hoeller * @since 3.1 @@ -40,4 +42,8 @@ public class HibernateOptimisticLockingFailureException extends ObjectOptimistic super(ex.getMessage(), ex); } + public HibernateOptimisticLockingFailureException(OptimisticEntityLockException ex) { + super(ex.getMessage(), ex); + } + } diff --git a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/SessionFactoryUtils.java b/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/SessionFactoryUtils.java index b714c667b84..fc464074e68 100644 --- a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/SessionFactoryUtils.java +++ b/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/SessionFactoryUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,8 +26,10 @@ import org.hibernate.NonUniqueObjectException; import org.hibernate.NonUniqueResultException; import org.hibernate.ObjectDeletedException; import org.hibernate.PersistentObjectException; +import org.hibernate.PessimisticLockException; import org.hibernate.PropertyValueException; import org.hibernate.QueryException; +import org.hibernate.QueryTimeoutException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.StaleObjectStateException; @@ -35,6 +37,8 @@ import org.hibernate.StaleStateException; import org.hibernate.TransientObjectException; import org.hibernate.UnresolvableObjectException; import org.hibernate.WrongClassException; +import org.hibernate.dialect.lock.OptimisticEntityLockException; +import org.hibernate.dialect.lock.PessimisticEntityLockException; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.exception.ConstraintViolationException; import org.hibernate.exception.DataException; @@ -51,6 +55,7 @@ import org.springframework.dao.DuplicateKeyException; import org.springframework.dao.IncorrectResultSizeDataAccessException; import org.springframework.dao.InvalidDataAccessApiUsageException; import org.springframework.dao.InvalidDataAccessResourceUsageException; +import org.springframework.dao.PessimisticLockingFailureException; import org.springframework.jdbc.datasource.DataSourceUtils; /** @@ -116,7 +121,7 @@ public abstract class SessionFactoryUtils { /** * Convert the given HibernateException to an appropriate exception * from the {@code org.springframework.dao} hierarchy. - * @param ex HibernateException that occured + * @param ex HibernateException that occurred * @return the corresponding DataAccessException instance * @see HibernateExceptionTranslator#convertHibernateAccessException * @see HibernateTransactionManager#convertHibernateAccessException @@ -129,10 +134,18 @@ public abstract class SessionFactoryUtils { SQLGrammarException jdbcEx = (SQLGrammarException) ex; return new InvalidDataAccessResourceUsageException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex); } + if (ex instanceof QueryTimeoutException) { + QueryTimeoutException jdbcEx = (QueryTimeoutException) ex; + return new org.springframework.dao.QueryTimeoutException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex); + } if (ex instanceof LockAcquisitionException) { LockAcquisitionException jdbcEx = (LockAcquisitionException) ex; return new CannotAcquireLockException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex); } + if (ex instanceof PessimisticLockException) { + PessimisticLockException jdbcEx = (PessimisticLockException) ex; + return new PessimisticLockingFailureException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex); + } if (ex instanceof ConstraintViolationException) { ConstraintViolationException jdbcEx = (ConstraintViolationException) ex; return new DataIntegrityViolationException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + @@ -180,6 +193,15 @@ public abstract class SessionFactoryUtils { if (ex instanceof StaleStateException) { return new HibernateOptimisticLockingFailureException((StaleStateException) ex); } + if (ex instanceof OptimisticEntityLockException) { + return new HibernateOptimisticLockingFailureException((OptimisticEntityLockException) ex); + } + if (ex instanceof PessimisticEntityLockException) { + if (ex.getCause() instanceof LockAcquisitionException) { + return new CannotAcquireLockException(ex.getMessage(), ex.getCause()); + } + return new PessimisticLockingFailureException(ex.getMessage(), ex); + } // fallback return new HibernateSystemException(ex);