HHH-13541 ExceptionConverter instance in AbstractSharedSessionContract should be lazily initialized

This commit is contained in:
Sanne Grinovero 2019-08-05 14:10:52 +01:00
parent 71fe4229b4
commit 8c228244de
4 changed files with 70 additions and 66 deletions

View File

@ -473,6 +473,12 @@ public interface SharedSessionContractImplementor
*/ */
LoadQueryInfluencers getLoadQueryInfluencers(); LoadQueryInfluencers getLoadQueryInfluencers();
/**
* The converter associated to a Session might be lazily initialized: only invoke
* this getter when there is actual need to use it.
*
* @return the ExceptionConverter for this Session.
*/
ExceptionConverter getExceptionConverter(); ExceptionConverter getExceptionConverter();
/** /**

View File

@ -30,7 +30,6 @@ public class TransactionImpl implements TransactionImplementor {
private static final Logger LOG = CoreLogging.logger( TransactionImpl.class ); private static final Logger LOG = CoreLogging.logger( TransactionImpl.class );
private final TransactionCoordinator transactionCoordinator; private final TransactionCoordinator transactionCoordinator;
private final ExceptionConverter exceptionConverter;
private final JpaCompliance jpaCompliance; private final JpaCompliance jpaCompliance;
private final AbstractSharedSessionContract session; private final AbstractSharedSessionContract session;
@ -38,10 +37,8 @@ public class TransactionImpl implements TransactionImplementor {
public TransactionImpl( public TransactionImpl(
TransactionCoordinator transactionCoordinator, TransactionCoordinator transactionCoordinator,
ExceptionConverter exceptionConverter,
AbstractSharedSessionContract session) { AbstractSharedSessionContract session) {
this.transactionCoordinator = transactionCoordinator; this.transactionCoordinator = transactionCoordinator;
this.exceptionConverter = exceptionConverter;
this.jpaCompliance = session.getFactory().getSessionFactoryOptions().getJpaCompliance(); this.jpaCompliance = session.getFactory().getSessionFactoryOptions().getJpaCompliance();
this.session = session; this.session = session;
@ -104,7 +101,7 @@ public class TransactionImpl implements TransactionImplementor {
internalGetTransactionDriverControl().commit(); internalGetTransactionDriverControl().commit();
} }
catch (RuntimeException e) { catch (RuntimeException e) {
throw exceptionConverter.convertCommitException( e ); throw session.getExceptionConverter().convertCommitException( e );
} }
} }

View File

@ -147,7 +147,8 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
private Integer jdbcBatchSize; private Integer jdbcBatchSize;
protected transient ExceptionConverter exceptionConverter; //Lazily initialized
private transient ExceptionConverter exceptionConverter;
private CriteriaCompiler criteriaCompiler; private CriteriaCompiler criteriaCompiler;
@ -217,7 +218,6 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
.getService( TransactionCoordinatorBuilder.class ) .getService( TransactionCoordinatorBuilder.class )
.buildTransactionCoordinator( jdbcCoordinator, this ); .buildTransactionCoordinator( jdbcCoordinator, this );
} }
exceptionConverter = new ExceptionConverterImpl( this );
} }
protected void addSharedSessionTransactionObserver(TransactionCoordinator transactionCoordinator) { protected void addSharedSessionTransactionObserver(TransactionCoordinator transactionCoordinator) {
@ -314,7 +314,7 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
} }
catch ( HibernateException e ) { catch ( HibernateException e ) {
if ( getFactory().getSessionFactoryOptions().isJpaBootstrap() ) { if ( getFactory().getSessionFactoryOptions().isJpaBootstrap() ) {
throw this.exceptionConverter.convert( e ); throw getExceptionConverter().convert( e );
} }
else { else {
throw e; throw e;
@ -436,7 +436,6 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
if ( this.currentHibernateTransaction == null ) { if ( this.currentHibernateTransaction == null ) {
this.currentHibernateTransaction = new TransactionImpl( this.currentHibernateTransaction = new TransactionImpl(
getTransactionCoordinator(), getTransactionCoordinator(),
getExceptionConverter(),
this this
); );
} }
@ -557,7 +556,7 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
return callback.executeOnConnection( connection ); return callback.executeOnConnection( connection );
} }
catch (SQLException e) { catch (SQLException e) {
throw exceptionConverter.convert( throw getExceptionConverter().convert(
e, e,
"Error creating contextual LOB : " + e.getMessage() "Error creating contextual LOB : " + e.getMessage()
); );
@ -644,7 +643,7 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
return createNativeQuery( nativeQueryDefinition, true ); return createNativeQuery( nativeQueryDefinition, true );
} }
throw exceptionConverter.convert( new IllegalArgumentException( "No query defined for that name [" + name + "]" ) ); throw getExceptionConverter().convert( new IllegalArgumentException( "No query defined for that name [" + name + "]" ) );
} }
protected QueryImplementor createQuery(NamedQueryDefinition queryDefinition) { protected QueryImplementor createQuery(NamedQueryDefinition queryDefinition) {
@ -737,7 +736,7 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
} }
catch (RuntimeException e) { catch (RuntimeException e) {
markForRollbackOnly(); markForRollbackOnly();
throw exceptionConverter.convert( e ); throw getExceptionConverter().convert( e );
} }
} }
@ -757,7 +756,7 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
return (QueryImplementor<T>) criteriaCompiler().compile( (CompilableCriteria) criteriaQuery ); return (QueryImplementor<T>) criteriaCompiler().compile( (CompilableCriteria) criteriaQuery );
} }
catch ( RuntimeException e ) { catch ( RuntimeException e ) {
throw exceptionConverter.convert( e ); throw getExceptionConverter().convert( e );
} }
} }
@ -768,7 +767,7 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
return criteriaCompiler().compile( (CompilableCriteria) criteriaUpdate ); return criteriaCompiler().compile( (CompilableCriteria) criteriaUpdate );
} }
catch ( RuntimeException e ) { catch ( RuntimeException e ) {
throw exceptionConverter.convert( e ); throw getExceptionConverter().convert( e );
} }
} }
@ -779,7 +778,7 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
return criteriaCompiler().compile( (CompilableCriteria) criteriaDelete ); return criteriaCompiler().compile( (CompilableCriteria) criteriaDelete );
} }
catch ( RuntimeException e ) { catch ( RuntimeException e ) {
throw exceptionConverter.convert( e ); throw getExceptionConverter().convert( e );
} }
} }
@ -812,7 +811,7 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
return query; return query;
} }
catch ( RuntimeException e ) { catch ( RuntimeException e ) {
throw exceptionConverter.convert( e ); throw getExceptionConverter().convert( e );
} }
} }
@ -833,7 +832,7 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
return query; return query;
} }
catch ( RuntimeException e ) { catch ( RuntimeException e ) {
throw exceptionConverter.convert( e ); throw getExceptionConverter().convert( e );
} }
} }
@ -911,7 +910,7 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
return (QueryImplementor<T>) createNativeQuery( nativeQueryDefinition, resultType ); return (QueryImplementor<T>) createNativeQuery( nativeQueryDefinition, resultType );
} }
throw exceptionConverter.convert( new IllegalArgumentException( "No query defined for that name [" + name + "]" ) ); throw getExceptionConverter().convert( new IllegalArgumentException( "No query defined for that name [" + name + "]" ) );
} }
catch (RuntimeException e) { catch (RuntimeException e) {
throw !( e instanceof IllegalArgumentException ) ? new IllegalArgumentException( e ) : e; throw !( e instanceof IllegalArgumentException ) ? new IllegalArgumentException( e ) : e;
@ -1029,7 +1028,7 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
return query; return query;
} }
catch ( RuntimeException he ) { catch ( RuntimeException he ) {
throw exceptionConverter.convert( he ); throw getExceptionConverter().convert( he );
} }
} }
@ -1054,7 +1053,7 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
return query; return query;
} }
catch ( RuntimeException he ) { catch ( RuntimeException he ) {
throw exceptionConverter.convert( he ); throw getExceptionConverter().convert( he );
} }
} }
@ -1069,7 +1068,7 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
return createNativeQuery( nativeQueryDefinition, true ); return createNativeQuery( nativeQueryDefinition, true );
} }
throw exceptionConverter.convert( new IllegalArgumentException( "No query defined for that name [" + name + "]" ) ); throw getExceptionConverter().convert( new IllegalArgumentException( "No query defined for that name [" + name + "]" ) );
} }
@Override @Override
@ -1095,7 +1094,7 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
return query; return query;
} }
catch ( RuntimeException he ) { catch ( RuntimeException he ) {
throw exceptionConverter.convert( he ); throw getExceptionConverter().convert( he );
} }
} }
@ -1161,7 +1160,10 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
} }
@Override @Override
public ExceptionConverter getExceptionConverter(){ public ExceptionConverter getExceptionConverter() {
if ( exceptionConverter == null ) {
exceptionConverter = new ExceptionConverterImpl( this );
}
return exceptionConverter; return exceptionConverter;
} }
@ -1234,8 +1236,7 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
.buildTransactionCoordinator( jdbcCoordinator, this ); .buildTransactionCoordinator( jdbcCoordinator, this );
entityNameResolver = new CoordinatingEntityNameResolver( factory, interceptor ); entityNameResolver = new CoordinatingEntityNameResolver( factory, interceptor );
exceptionConverter = new ExceptionConverterImpl( this );
this.disallowOutOfTransactionUpdateOperations = !getFactory().getSessionFactoryOptions().isAllowOutOfTransactionUpdateOperations(); this.disallowOutOfTransactionUpdateOperations = !getFactory().getSessionFactoryOptions().isAllowOutOfTransactionUpdateOperations();
} }
} }

View File

@ -370,7 +370,7 @@ public final class SessionImpl
internalClear(); internalClear();
} }
catch (RuntimeException e) { catch (RuntimeException e) {
throw exceptionConverter.convert( e ); throw getExceptionConverter().convert( e );
} }
} }
@ -472,7 +472,7 @@ public final class SessionImpl
return !isClosed(); return !isClosed();
} }
catch (HibernateException he) { catch (HibernateException he) {
throw exceptionConverter.convert( he ); throw getExceptionConverter().convert( he );
} }
} }
@ -789,17 +789,17 @@ public final class SessionImpl
} }
} }
catch (MappingException e) { catch (MappingException e) {
throw exceptionConverter.convert( new IllegalArgumentException( e.getMessage() ) ); throw getExceptionConverter().convert( new IllegalArgumentException( e.getMessage() ) );
} }
catch (RuntimeException e) { catch (RuntimeException e) {
throw exceptionConverter.convert( e ); throw getExceptionConverter().convert( e );
} }
finally { finally {
try { try {
checkNoUnresolvedActionsAfterOperation(); checkNoUnresolvedActionsAfterOperation();
} }
catch (RuntimeException e) { catch (RuntimeException e) {
throw exceptionConverter.convert( e ); throw getExceptionConverter().convert( e );
} }
} }
} }
@ -813,10 +813,10 @@ public final class SessionImpl
} }
} }
catch ( MappingException e ) { catch ( MappingException e ) {
throw exceptionConverter.convert( new IllegalArgumentException( e.getMessage() ) ) ; throw getExceptionConverter().convert( new IllegalArgumentException( e.getMessage() ) ) ;
} }
catch ( RuntimeException e ) { catch ( RuntimeException e ) {
throw exceptionConverter.convert( e ); throw getExceptionConverter().convert( e );
} }
finally { finally {
delayedAfterCompletion(); delayedAfterCompletion();
@ -891,14 +891,14 @@ public final class SessionImpl
checkNoUnresolvedActionsAfterOperation(); checkNoUnresolvedActionsAfterOperation();
} }
catch ( ObjectDeletedException sse ) { catch ( ObjectDeletedException sse ) {
throw exceptionConverter.convert( new IllegalArgumentException( sse ) ); throw getExceptionConverter().convert( new IllegalArgumentException( sse ) );
} }
catch ( MappingException e ) { catch ( MappingException e ) {
throw exceptionConverter.convert( new IllegalArgumentException( e.getMessage(), e ) ); throw getExceptionConverter().convert( new IllegalArgumentException( e.getMessage(), e ) );
} }
catch ( RuntimeException e ) { catch ( RuntimeException e ) {
//including HibernateException //including HibernateException
throw exceptionConverter.convert( e ); throw getExceptionConverter().convert( e );
} }
return event.getResult(); return event.getResult();
@ -912,14 +912,14 @@ public final class SessionImpl
} }
} }
catch ( ObjectDeletedException sse ) { catch ( ObjectDeletedException sse ) {
throw exceptionConverter.convert( new IllegalArgumentException( sse ) ); throw getExceptionConverter().convert( new IllegalArgumentException( sse ) );
} }
catch ( MappingException e ) { catch ( MappingException e ) {
throw exceptionConverter.convert( new IllegalArgumentException( e.getMessage(), e ) ); throw getExceptionConverter().convert( new IllegalArgumentException( e.getMessage(), e ) );
} }
catch ( RuntimeException e ) { catch ( RuntimeException e ) {
//including HibernateException //including HibernateException
throw exceptionConverter.convert( e ); throw getExceptionConverter().convert( e );
} }
finally { finally {
delayedAfterCompletion(); delayedAfterCompletion();
@ -1000,14 +1000,14 @@ public final class SessionImpl
} }
} }
catch ( ObjectDeletedException sse ) { catch ( ObjectDeletedException sse ) {
throw exceptionConverter.convert( new IllegalArgumentException( sse ) ); throw getExceptionConverter().convert( new IllegalArgumentException( sse ) );
} }
catch ( MappingException e ) { catch ( MappingException e ) {
throw exceptionConverter.convert( new IllegalArgumentException( e.getMessage(), e ) ); throw getExceptionConverter().convert( new IllegalArgumentException( e.getMessage(), e ) );
} }
catch ( RuntimeException e ) { catch ( RuntimeException e ) {
//including HibernateException //including HibernateException
throw exceptionConverter.convert( e ); throw getExceptionConverter().convert( e );
} }
finally { finally {
delayedAfterCompletion(); delayedAfterCompletion();
@ -1022,14 +1022,14 @@ public final class SessionImpl
} }
} }
catch ( ObjectDeletedException sse ) { catch ( ObjectDeletedException sse ) {
throw exceptionConverter.convert( new IllegalArgumentException( sse ) ); throw getExceptionConverter().convert( new IllegalArgumentException( sse ) );
} }
catch ( MappingException e ) { catch ( MappingException e ) {
throw exceptionConverter.convert( new IllegalArgumentException( e.getMessage(), e ) ); throw getExceptionConverter().convert( new IllegalArgumentException( e.getMessage(), e ) );
} }
catch ( RuntimeException e ) { catch ( RuntimeException e ) {
//including HibernateException //including HibernateException
throw exceptionConverter.convert( e ); throw getExceptionConverter().convert( e );
} }
finally { finally {
delayedAfterCompletion(); delayedAfterCompletion();
@ -1373,7 +1373,7 @@ public final class SessionImpl
} }
} }
//including HibernateException //including HibernateException
throw exceptionConverter.convert( e ); throw getExceptionConverter().convert( e );
} }
finally { finally {
delayedAfterCompletion(); delayedAfterCompletion();
@ -1388,7 +1388,7 @@ public final class SessionImpl
} }
} }
catch (RuntimeException e) { catch (RuntimeException e) {
throw exceptionConverter.convert( e ); throw getExceptionConverter().convert( e );
} }
finally { finally {
delayedAfterCompletion(); delayedAfterCompletion();
@ -1496,7 +1496,7 @@ public final class SessionImpl
delayedAfterCompletion(); delayedAfterCompletion();
} }
catch ( RuntimeException e ) { catch ( RuntimeException e ) {
throw exceptionConverter.convert( e ); throw getExceptionConverter().convert( e );
} }
} }
@ -2152,7 +2152,7 @@ public final class SessionImpl
throw new IllegalArgumentException( e.getMessage(), e ); throw new IllegalArgumentException( e.getMessage(), e );
} }
catch (RuntimeException e) { catch (RuntimeException e) {
throw exceptionConverter.convert( e ); throw getExceptionConverter().convert( e );
} }
} }
@ -2206,7 +2206,7 @@ public final class SessionImpl
throw new IllegalArgumentException( e.getMessage(), e ); throw new IllegalArgumentException( e.getMessage(), e );
} }
catch (RuntimeException e) { catch (RuntimeException e) {
throw exceptionConverter.convert( e ); throw getExceptionConverter().convert( e );
} }
} }
@ -3491,11 +3491,11 @@ public final class SessionImpl
delete( entity ); delete( entity );
} }
catch (MappingException e) { catch (MappingException e) {
throw exceptionConverter.convert( new IllegalArgumentException( e.getMessage(), e ) ); throw getExceptionConverter().convert( new IllegalArgumentException( e.getMessage(), e ) );
} }
catch ( RuntimeException e ) { catch ( RuntimeException e ) {
//including HibernateException //including HibernateException
throw exceptionConverter.convert( e ); throw getExceptionConverter().convert( e );
} }
} }
@ -3555,7 +3555,7 @@ public final class SessionImpl
throw new IllegalArgumentException( e.getMessage(), e ); throw new IllegalArgumentException( e.getMessage(), e );
} }
catch ( MappingException | TypeMismatchException | ClassCastException e ) { catch ( MappingException | TypeMismatchException | ClassCastException e ) {
throw exceptionConverter.convert( new IllegalArgumentException( e.getMessage(), e ) ); throw getExceptionConverter().convert( new IllegalArgumentException( e.getMessage(), e ) );
} }
catch ( JDBCException e ) { catch ( JDBCException e ) {
if ( accessTransaction().getRollbackOnly() ) { if ( accessTransaction().getRollbackOnly() ) {
@ -3563,11 +3563,11 @@ public final class SessionImpl
return null; return null;
} }
else { else {
throw exceptionConverter.convert( e, lockOptions ); throw getExceptionConverter().convert( e, lockOptions );
} }
} }
catch ( RuntimeException e ) { catch ( RuntimeException e ) {
throw exceptionConverter.convert( e, lockOptions ); throw getExceptionConverter().convert( e, lockOptions );
} }
finally { finally {
getLoadQueryInfluencers().getEffectiveEntityGraph().clear(); getLoadQueryInfluencers().getEffectiveEntityGraph().clear();
@ -3612,10 +3612,10 @@ public final class SessionImpl
return byId( entityClass ).getReference( (Serializable) primaryKey ); return byId( entityClass ).getReference( (Serializable) primaryKey );
} }
catch ( MappingException | TypeMismatchException | ClassCastException e ) { catch ( MappingException | TypeMismatchException | ClassCastException e ) {
throw exceptionConverter.convert( new IllegalArgumentException( e.getMessage(), e ) ); throw getExceptionConverter().convert( new IllegalArgumentException( e.getMessage(), e ) );
} }
catch ( RuntimeException e ) { catch ( RuntimeException e ) {
throw exceptionConverter.convert( e ); throw getExceptionConverter().convert( e );
} }
} }
@ -3638,7 +3638,7 @@ public final class SessionImpl
buildLockRequest( lockOptions ).lock( entity ); buildLockRequest( lockOptions ).lock( entity );
} }
catch (RuntimeException e) { catch (RuntimeException e) {
throw exceptionConverter.convert( e, lockOptions ); throw getExceptionConverter().convert( e, lockOptions );
} }
} }
@ -3664,7 +3664,7 @@ public final class SessionImpl
setCacheMode( refreshCacheMode ); setCacheMode( refreshCacheMode );
if ( !contains( entity ) ) { if ( !contains( entity ) ) {
throw exceptionConverter.convert( new IllegalArgumentException( "Entity not managed" ) ); throw getExceptionConverter().convert( new IllegalArgumentException( "Entity not managed" ) );
} }
if ( lockModeType != null ) { if ( lockModeType != null ) {
@ -3680,10 +3680,10 @@ public final class SessionImpl
} }
} }
catch (MappingException e) { catch (MappingException e) {
throw exceptionConverter.convert( new IllegalArgumentException( e.getMessage(), e ) ); throw getExceptionConverter().convert( new IllegalArgumentException( e.getMessage(), e ) );
} }
catch (RuntimeException e) { catch (RuntimeException e) {
throw exceptionConverter.convert( e, lockOptions ); throw getExceptionConverter().convert( e, lockOptions );
} }
finally { finally {
setCacheMode( previousCacheMode ); setCacheMode( previousCacheMode );
@ -3697,7 +3697,7 @@ public final class SessionImpl
evict( entity ); evict( entity );
} }
catch (RuntimeException e) { catch (RuntimeException e) {
throw exceptionConverter.convert( e ); throw getExceptionConverter().convert( e );
} }
} }
@ -3710,7 +3710,7 @@ public final class SessionImpl
} }
if ( !contains( entity ) ) { if ( !contains( entity ) ) {
throw exceptionConverter.convert( new IllegalArgumentException( "entity not in the persistence context" ) ); throw getExceptionConverter().convert( new IllegalArgumentException( "entity not in the persistence context" ) );
} }
return LockModeTypeHelper.getLockModeType( getCurrentLockMode( entity ) ); return LockModeTypeHelper.getLockModeType( getCurrentLockMode( entity ) );
@ -3759,7 +3759,7 @@ public final class SessionImpl
return memento.makeProcedureCall( this ); return memento.makeProcedureCall( this );
} }
catch ( RuntimeException e ) { catch ( RuntimeException e ) {
throw exceptionConverter.convert( e ); throw getExceptionConverter().convert( e );
} }
} }
@ -3769,7 +3769,7 @@ public final class SessionImpl
return createStoredProcedureCall( procedureName ); return createStoredProcedureCall( procedureName );
} }
catch ( RuntimeException e ) { catch ( RuntimeException e ) {
throw exceptionConverter.convert( e ); throw getExceptionConverter().convert( e );
} }
} }
@ -3779,7 +3779,7 @@ public final class SessionImpl
return createStoredProcedureCall( procedureName, resultClasses ); return createStoredProcedureCall( procedureName, resultClasses );
} }
catch ( RuntimeException e ) { catch ( RuntimeException e ) {
throw exceptionConverter.convert( e ); throw getExceptionConverter().convert( e );
} }
} }
@ -3795,7 +3795,7 @@ public final class SessionImpl
} }
} }
catch ( RuntimeException e ) { catch ( RuntimeException e ) {
throw exceptionConverter.convert( e ); throw getExceptionConverter().convert( e );
} }
} }
@ -3820,7 +3820,7 @@ public final class SessionImpl
throw new TransactionRequiredException( e.getMessage() ); throw new TransactionRequiredException( e.getMessage() );
} }
catch (HibernateException he) { catch (HibernateException he) {
throw exceptionConverter.convert( he ); throw getExceptionConverter().convert( he );
} }
} }