Explicit support for Hibernate 4.0's QueryTimeoutException, PessimisticLockException, PessimisticEntityLockException and OptimisticEntityLockException

Issue: SPR-10815
This commit is contained in:
Juergen Hoeller 2013-08-20 19:06:24 +02:00
parent 3b211335c7
commit c762b83a76
2 changed files with 32 additions and 4 deletions

View File

@ -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);
}
}

View File

@ -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);