clean up ExceptionConverterImpl

Signed-off-by: Gavin King <gavin@hibernate.org>
This commit is contained in:
Gavin King 2024-09-01 15:32:29 +02:00
parent dd16ed61f0
commit d7aa5f7a78
3 changed files with 50 additions and 78 deletions

View File

@ -11,7 +11,6 @@ import org.checkerframework.checker.nullness.qual.Nullable;
import org.hibernate.HibernateException;
import org.hibernate.TransactionException;
import org.hibernate.engine.spi.ExceptionConverter;
import org.hibernate.engine.transaction.spi.TransactionImplementor;
import org.hibernate.internal.AbstractSharedSessionContract;
import org.hibernate.internal.CoreLogging;

View File

@ -46,12 +46,12 @@ import jakarta.persistence.RollbackException;
public class ExceptionConverterImpl implements ExceptionConverter {
private static final EntityManagerMessageLogger log = HEMLogging.messageLogger( ExceptionConverterImpl.class );
private final SharedSessionContractImplementor sharedSessionContract;
private final SharedSessionContractImplementor session;
private final boolean isJpaBootstrap;
public ExceptionConverterImpl(SharedSessionContractImplementor sharedSessionContract) {
this.sharedSessionContract = sharedSessionContract;
isJpaBootstrap = sharedSessionContract.getFactory().getSessionFactoryOptions().isJpaBootstrap();
public ExceptionConverterImpl(SharedSessionContractImplementor session) {
this.session = session;
isJpaBootstrap = session.getFactory().getSessionFactoryOptions().isJpaBootstrap();
}
@Override
@ -59,30 +59,15 @@ public class ExceptionConverterImpl implements ExceptionConverter {
if ( isJpaBootstrap ) {
try {
//as per the spec we should roll back if commit fails
sharedSessionContract.getTransaction().rollback();
session.getTransaction().rollback();
}
catch (Exception re) {
//swallow
}
return new RollbackException( "Error while committing the transaction", wrapCommitException( exception ) );
}
else {
return exception;
}
}
private Throwable wrapCommitException(RuntimeException exception) {
if ( exception instanceof HibernateException ) {
return convert( (HibernateException) exception);
}
else if ( exception instanceof PersistenceException ) {
Throwable cause = exception.getCause() == null ? exception : exception.getCause();
if ( cause instanceof HibernateException ) {
return convert( (HibernateException) cause );
}
else {
return cause;
}
return new RollbackException( "Error while committing the transaction",
exception instanceof HibernateException hibernateException
? convert( hibernateException )
: exception );
}
else {
return exception;
@ -147,7 +132,7 @@ public class ExceptionConverterImpl implements ExceptionConverter {
}
else if ( exception instanceof TransientObjectException ) {
try {
sharedSessionContract.markForRollbackOnly();
session.markForRollbackOnly();
}
catch (Exception ne) {
//we do not want the subsequent exception to swallow the original one
@ -169,99 +154,78 @@ public class ExceptionConverterImpl implements ExceptionConverter {
@Override
public RuntimeException convert(RuntimeException exception) {
if ( exception instanceof HibernateException ) {
return convert( (HibernateException) exception );
if ( exception instanceof HibernateException hibernateException ) {
return convert( hibernateException );
}
else {
sharedSessionContract.markForRollbackOnly();
session.markForRollbackOnly();
return exception;
}
}
@Override
public RuntimeException convert(RuntimeException exception, LockOptions lockOptions) {
if ( exception instanceof HibernateException ) {
return convert( (HibernateException) exception, lockOptions );
if ( exception instanceof HibernateException hibernateException ) {
return convert( hibernateException, lockOptions );
}
else {
sharedSessionContract.markForRollbackOnly();
session.markForRollbackOnly();
return exception;
}
}
@Override
public JDBCException convert(SQLException e, String message) {
return sharedSessionContract.getJdbcServices().getSqlExceptionHelper().convert( e, message );
return session.getJdbcServices().getSqlExceptionHelper().convert( e, message );
}
protected PersistenceException wrapStaleStateException(StaleStateException exception) {
if ( exception instanceof StaleObjectStateException ) {
final StaleObjectStateException sose = (StaleObjectStateException) exception;
final Object identifier = sose.getIdentifier();
if ( exception instanceof StaleObjectStateException staleStateException ) {
final Object identifier = staleStateException.getIdentifier();
final String entityName = staleStateException.getEntityName();
if ( identifier != null ) {
try {
final Object entity = sharedSessionContract.internalLoad( sose.getEntityName(), identifier, false, true);
if ( entity instanceof Serializable ) {
//avoid some user errors regarding boundary crossing
final Object entity = session.internalLoad( entityName, identifier, false, true );
if ( entity instanceof Serializable ) { // avoid some user errors regarding boundary crossing
return new OptimisticLockException( exception.getMessage(), exception, entity );
}
else {
}
catch (EntityNotFoundException entityNotFoundException) {
// swallow it;
}
}
}
return new OptimisticLockException( exception.getMessage(), exception );
}
}
catch (EntityNotFoundException enfe) {
return new OptimisticLockException( exception.getMessage(), exception );
}
}
else {
return new OptimisticLockException( exception.getMessage(), exception );
}
}
else {
return new OptimisticLockException( exception.getMessage(), exception );
}
}
protected PersistenceException wrapLockException(HibernateException exception, LockOptions lockOptions) {
if ( exception instanceof OptimisticEntityLockException ) {
final OptimisticEntityLockException lockException = (OptimisticEntityLockException) exception;
if ( exception instanceof OptimisticEntityLockException lockException ) {
return new OptimisticLockException( lockException.getMessage(), lockException, lockException.getEntity() );
}
else if ( exception instanceof org.hibernate.exception.LockTimeoutException ) {
return new LockTimeoutException( exception.getMessage(), exception, null );
}
else if ( exception instanceof PessimisticEntityLockException ) {
final PessimisticEntityLockException lockException = (PessimisticEntityLockException) exception;
if ( lockOptions != null && lockOptions.getTimeOut() > -1 ) {
else if ( exception instanceof PessimisticEntityLockException lockException ) {
// assume lock timeout occurred if a timeout or NO WAIT was specified
return new LockTimeoutException( lockException.getMessage(), lockException, lockException.getEntity() );
return lockOptions != null && lockOptions.getTimeOut() > -1
? new LockTimeoutException( lockException.getMessage(), lockException, lockException.getEntity() )
: new PessimisticLockException( lockException.getMessage(), lockException, lockException.getEntity() );
}
else {
return new PessimisticLockException( lockException.getMessage(), lockException, lockException.getEntity() );
}
}
else if ( exception instanceof org.hibernate.PessimisticLockException ) {
final org.hibernate.PessimisticLockException lockException = (org.hibernate.PessimisticLockException) exception;
if ( lockOptions != null && lockOptions.getTimeOut() > -1 ) {
else if ( exception instanceof org.hibernate.PessimisticLockException lockException ) {
// assume lock timeout occurred if a timeout or NO WAIT was specified
return new LockTimeoutException( lockException.getMessage(), lockException, null );
}
else {
return new PessimisticLockException( lockException.getMessage(), lockException, null );
}
return lockOptions != null && lockOptions.getTimeOut() > -1
? new LockTimeoutException( lockException.getMessage(), lockException, null )
: new PessimisticLockException( lockException.getMessage(), lockException, null );
}
else {
return new OptimisticLockException( exception );
}
}
private void rollbackIfNecessary(PersistenceException e) {
if ( !( e instanceof NoResultException
|| e instanceof NonUniqueResultException
|| e instanceof LockTimeoutException
|| e instanceof QueryTimeoutException ) ) {
private void rollbackIfNecessary(PersistenceException persistenceException) {
if ( !isNonRollbackException( persistenceException ) ) {
try {
sharedSessionContract.markForRollbackOnly();
session.markForRollbackOnly();
}
catch (Exception ne) {
//we do not want the subsequent exception to swallow the original one
@ -270,4 +234,15 @@ public class ExceptionConverterImpl implements ExceptionConverter {
}
}
/**
* Is this a special exception type explicitly exempted from
* forced rollbacks by the JPA specification itself?
*/
private static boolean isNonRollbackException(PersistenceException persistenceException) {
return persistenceException instanceof NoResultException
|| persistenceException instanceof NonUniqueResultException
|| persistenceException instanceof LockTimeoutException
|| persistenceException instanceof QueryTimeoutException;
}
}

View File

@ -11,8 +11,6 @@ import org.jboss.logging.Logger;
import java.lang.invoke.MethodHandles;
/**
* Sad when you need helpers for generating loggers...
*
* @author Steve Ebersole
*/
public class HEMLogging {