get rid of ugly message

+ refresh code in ExceptionConverterImpl
This commit is contained in:
Gavin King 2022-10-07 13:04:08 +02:00
parent 2a9f6dafee
commit 635c23bc4a
1 changed files with 68 additions and 98 deletions

View File

@ -25,8 +25,6 @@ import org.hibernate.engine.spi.ExceptionConverter;
import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.exception.LockAcquisitionException; import org.hibernate.exception.LockAcquisitionException;
import org.hibernate.loader.MultipleBagFetchException; import org.hibernate.loader.MultipleBagFetchException;
import org.hibernate.query.SemanticException;
import org.hibernate.query.sqm.InterpretationException;
import org.hibernate.query.sqm.ParsingException; import org.hibernate.query.sqm.ParsingException;
import jakarta.persistence.EntityExistsException; import jakarta.persistence.EntityExistsException;
@ -55,35 +53,37 @@ public class ExceptionConverterImpl implements ExceptionConverter {
} }
@Override @Override
public RuntimeException convertCommitException(RuntimeException e) { public RuntimeException convertCommitException(RuntimeException exception) {
if ( isJpaBootstrap ) { if ( isJpaBootstrap ) {
Throwable wrappedException;
if ( e instanceof HibernateException ) {
wrappedException = convert( (HibernateException) e );
}
else if ( e instanceof PersistenceException ) {
Throwable cause = e.getCause() == null ? e : e.getCause();
if ( cause instanceof HibernateException ) {
wrappedException = convert( (HibernateException) cause );
}
else {
wrappedException = cause;
}
}
else {
wrappedException = e;
}
try { try {
//as per the spec we should rollback if commit fails //as per the spec we should roll back if commit fails
sharedSessionContract.getTransaction().rollback(); sharedSessionContract.getTransaction().rollback();
} }
catch (Exception re) { catch (Exception re) {
//swallow //swallow
} }
return new RollbackException( "Error while committing the transaction", wrappedException ); return new RollbackException( "Error while committing the transaction", wrapCommitException( exception ) );
} }
else { else {
return e; 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;
}
}
else {
return exception;
} }
} }
@ -91,59 +91,50 @@ public class ExceptionConverterImpl implements ExceptionConverter {
public RuntimeException convert(HibernateException exception, LockOptions lockOptions) { public RuntimeException convert(HibernateException exception, LockOptions lockOptions) {
if ( exception instanceof StaleStateException ) { if ( exception instanceof StaleStateException ) {
final PersistenceException converted = wrapStaleStateException( (StaleStateException) exception ); final PersistenceException converted = wrapStaleStateException( (StaleStateException) exception );
handlePersistenceException( converted ); rollbackIfNecessary( converted );
return converted; return converted;
} }
else if ( exception instanceof LockAcquisitionException ) { else if ( exception instanceof LockAcquisitionException ) {
final PersistenceException converted = wrapLockException( exception, lockOptions ); final PersistenceException converted = wrapLockException( exception, lockOptions );
handlePersistenceException( converted ); rollbackIfNecessary( converted );
return converted; return converted;
} }
else if ( exception instanceof LockingStrategyException ) { else if ( exception instanceof LockingStrategyException ) {
final PersistenceException converted = wrapLockException( exception, lockOptions ); final PersistenceException converted = wrapLockException( exception, lockOptions );
handlePersistenceException( converted ); rollbackIfNecessary( converted );
return converted; return converted;
} }
else if ( exception instanceof org.hibernate.PessimisticLockException ) { else if ( exception instanceof org.hibernate.PessimisticLockException ) {
final PersistenceException converted = wrapLockException( exception, lockOptions ); final PersistenceException converted = wrapLockException( exception, lockOptions );
handlePersistenceException( converted ); rollbackIfNecessary( converted );
return converted; return converted;
} }
else if ( exception instanceof org.hibernate.QueryTimeoutException ) { else if ( exception instanceof org.hibernate.QueryTimeoutException ) {
final QueryTimeoutException converted = new QueryTimeoutException( exception.getMessage(), exception ); final QueryTimeoutException converted = new QueryTimeoutException( exception.getMessage(), exception );
handlePersistenceException( converted ); rollbackIfNecessary( converted );
return converted; return converted;
} }
else if ( exception instanceof ObjectNotFoundException ) { else if ( exception instanceof ObjectNotFoundException ) {
final EntityNotFoundException converted = new EntityNotFoundException( exception.getMessage() ); final EntityNotFoundException converted = new EntityNotFoundException( exception.getMessage() );
handlePersistenceException( converted ); rollbackIfNecessary( converted );
return converted; return converted;
} }
else if ( exception instanceof org.hibernate.NonUniqueObjectException ) { else if ( exception instanceof org.hibernate.NonUniqueObjectException ) {
final EntityExistsException converted = new EntityExistsException( exception.getMessage() ); final EntityExistsException converted = new EntityExistsException( exception.getMessage() );
handlePersistenceException( converted ); rollbackIfNecessary( converted );
return converted; return converted;
} }
else if ( exception instanceof org.hibernate.NonUniqueResultException ) { else if ( exception instanceof org.hibernate.NonUniqueResultException ) {
final NonUniqueResultException converted = new NonUniqueResultException( exception.getMessage() ); final NonUniqueResultException converted = new NonUniqueResultException( exception.getMessage() );
handlePersistenceException( converted ); rollbackIfNecessary( converted );
return converted; return converted;
} }
else if ( exception instanceof UnresolvableObjectException ) { else if ( exception instanceof UnresolvableObjectException ) {
final EntityNotFoundException converted = new EntityNotFoundException( exception.getMessage() ); final EntityNotFoundException converted = new EntityNotFoundException( exception.getMessage() );
handlePersistenceException( converted ); rollbackIfNecessary( converted );
return converted; return converted;
} }
else if ( exception instanceof SemanticException ) { else if ( exception instanceof QueryException || exception instanceof ParsingException) {
return new IllegalArgumentException( exception );
}
else if ( exception instanceof QueryException ) {
return new IllegalArgumentException( exception );
}
else if ( exception instanceof InterpretationException ) {
return new IllegalArgumentException( exception );
}
else if ( exception instanceof ParsingException ) {
return new IllegalArgumentException( exception ); return new IllegalArgumentException( exception );
} }
else if ( exception instanceof MultipleBagFetchException ) { else if ( exception instanceof MultipleBagFetchException ) {
@ -161,42 +152,37 @@ public class ExceptionConverterImpl implements ExceptionConverter {
return new IllegalStateException( exception ); return new IllegalStateException( exception );
} }
else { else {
final PersistenceException converted = new PersistenceException( final PersistenceException converted = new PersistenceException( exception.getMessage(), exception );
"Converting `" + exception.getClass().getName() + "` to JPA `PersistenceException` : " + exception.getMessage(), rollbackIfNecessary( converted );
exception
);
handlePersistenceException( converted );
return converted; return converted;
} }
} }
@Override @Override
public RuntimeException convert(HibernateException e) { public RuntimeException convert(HibernateException exception) {
return convert( e, null ); return convert( exception, null );
} }
@Override @Override
public RuntimeException convert(RuntimeException e) { public RuntimeException convert(RuntimeException exception) {
RuntimeException result = e; if ( exception instanceof HibernateException ) {
if ( e instanceof HibernateException ) { return convert( (HibernateException) exception );
result = convert( (HibernateException) e );
} }
else { else {
sharedSessionContract.markForRollbackOnly(); sharedSessionContract.markForRollbackOnly();
return exception;
} }
return result;
} }
@Override @Override
public RuntimeException convert(RuntimeException e, LockOptions lockOptions) { public RuntimeException convert(RuntimeException exception, LockOptions lockOptions) {
RuntimeException result = e; if ( exception instanceof HibernateException ) {
if ( e instanceof HibernateException ) { return convert( (HibernateException) exception, lockOptions );
result = convert( (HibernateException) e, lockOptions );
} }
else { else {
sharedSessionContract.markForRollbackOnly(); sharedSessionContract.markForRollbackOnly();
return exception;
} }
return result;
} }
@Override @Override
@ -205,7 +191,6 @@ public class ExceptionConverterImpl implements ExceptionConverter {
} }
protected PersistenceException wrapStaleStateException(StaleStateException e) { protected PersistenceException wrapStaleStateException(StaleStateException e) {
PersistenceException pe;
if ( e instanceof StaleObjectStateException ) { if ( e instanceof StaleObjectStateException ) {
final StaleObjectStateException sose = (StaleObjectStateException) e; final StaleObjectStateException sose = (StaleObjectStateException) e;
final Object identifier = sose.getIdentifier(); final Object identifier = sose.getIdentifier();
@ -214,85 +199,70 @@ public class ExceptionConverterImpl implements ExceptionConverter {
final Object entity = sharedSessionContract.internalLoad( sose.getEntityName(), identifier, false, true); final Object entity = sharedSessionContract.internalLoad( sose.getEntityName(), identifier, false, true);
if ( entity instanceof Serializable ) { if ( entity instanceof Serializable ) {
//avoid some user errors regarding boundary crossing //avoid some user errors regarding boundary crossing
pe = new OptimisticLockException( e.getMessage(), e, entity ); return new OptimisticLockException( e.getMessage(), e, entity );
} }
else { else {
pe = new OptimisticLockException( e.getMessage(), e ); return new OptimisticLockException( e.getMessage(), e );
} }
} }
catch (EntityNotFoundException enfe) { catch (EntityNotFoundException enfe) {
pe = new OptimisticLockException( e.getMessage(), e ); return new OptimisticLockException( e.getMessage(), e );
} }
} }
else { else {
pe = new OptimisticLockException( e.getMessage(), e ); return new OptimisticLockException( e.getMessage(), e );
} }
} }
else { else {
pe = new OptimisticLockException( e.getMessage(), e ); return new OptimisticLockException( e.getMessage(), e );
} }
return pe;
} }
protected PersistenceException wrapLockException(HibernateException e, LockOptions lockOptions) { protected PersistenceException wrapLockException(HibernateException e, LockOptions lockOptions) {
final PersistenceException pe;
if ( e instanceof OptimisticEntityLockException ) { if ( e instanceof OptimisticEntityLockException ) {
final OptimisticEntityLockException lockException = (OptimisticEntityLockException) e; final OptimisticEntityLockException lockException = (OptimisticEntityLockException) e;
pe = new OptimisticLockException( lockException.getMessage(), lockException, lockException.getEntity() ); return new OptimisticLockException( lockException.getMessage(), lockException, lockException.getEntity() );
} }
else if ( e instanceof org.hibernate.exception.LockTimeoutException ) { else if ( e instanceof org.hibernate.exception.LockTimeoutException ) {
pe = new LockTimeoutException( e.getMessage(), e, null ); return new LockTimeoutException( e.getMessage(), e, null );
} }
else if ( e instanceof PessimisticEntityLockException ) { else if ( e instanceof PessimisticEntityLockException ) {
final PessimisticEntityLockException lockException = (PessimisticEntityLockException) e; final PessimisticEntityLockException lockException = (PessimisticEntityLockException) e;
if ( lockOptions != null && lockOptions.getTimeOut() > -1 ) { if ( lockOptions != null && lockOptions.getTimeOut() > -1 ) {
// assume lock timeout occurred if a timeout or NO WAIT was specified // assume lock timeout occurred if a timeout or NO WAIT was specified
pe = new LockTimeoutException( lockException.getMessage(), lockException, lockException.getEntity() ); return new LockTimeoutException( lockException.getMessage(), lockException, lockException.getEntity() );
} }
else { else {
pe = new PessimisticLockException( return new PessimisticLockException( lockException.getMessage(), lockException, lockException.getEntity() );
lockException.getMessage(),
lockException,
lockException.getEntity()
);
} }
} }
else if ( e instanceof org.hibernate.PessimisticLockException ) { else if ( e instanceof org.hibernate.PessimisticLockException ) {
final org.hibernate.PessimisticLockException jdbcLockException = (org.hibernate.PessimisticLockException) e; final org.hibernate.PessimisticLockException jdbcLockException = (org.hibernate.PessimisticLockException) e;
if ( lockOptions != null && lockOptions.getTimeOut() > -1 ) { if ( lockOptions != null && lockOptions.getTimeOut() > -1 ) {
// assume lock timeout occurred if a timeout or NO WAIT was specified // assume lock timeout occurred if a timeout or NO WAIT was specified
pe = new LockTimeoutException( jdbcLockException.getMessage(), jdbcLockException, null ); return new LockTimeoutException( jdbcLockException.getMessage(), jdbcLockException, null );
} }
else { else {
pe = new PessimisticLockException( jdbcLockException.getMessage(), jdbcLockException, null ); return new PessimisticLockException( jdbcLockException.getMessage(), jdbcLockException, null );
} }
} }
else { else {
pe = new OptimisticLockException( e ); return new OptimisticLockException( e );
} }
return pe;
} }
private void handlePersistenceException(PersistenceException e) { private void rollbackIfNecessary(PersistenceException e) {
if ( e instanceof NoResultException ) { if ( !( e instanceof NoResultException
return; || e instanceof NonUniqueResultException
} || e instanceof LockTimeoutException
if ( e instanceof NonUniqueResultException ) { || e instanceof QueryTimeoutException ) ) {
return; try {
} sharedSessionContract.markForRollbackOnly();
if ( e instanceof LockTimeoutException ) { }
return; catch (Exception ne) {
} //we do not want the subsequent exception to swallow the original one
if ( e instanceof QueryTimeoutException ) { log.unableToMarkForRollbackOnPersistenceException( ne );
return; }
}
try {
sharedSessionContract.markForRollbackOnly();
}
catch (Exception ne) {
//we do not want the subsequent exception to swallow the original one
log.unableToMarkForRollbackOnPersistenceException( ne );
} }
} }