Skip to content

Commit

Permalink
HHH-13541 ExceptionConverter instance in AbstractSharedSessionContrac…
Browse files Browse the repository at this point in the history
…t should be lazily initialized
  • Loading branch information
Sanne committed Aug 5, 2019
1 parent 71fe422 commit 8c22824
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 66 deletions.
Expand Up @@ -473,6 +473,12 @@ default boolean isQueryParametersValidationEnabled(){
*/
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();

/**
Expand Down
Expand Up @@ -30,18 +30,15 @@ public class TransactionImpl implements TransactionImplementor {
private static final Logger LOG = CoreLogging.logger( TransactionImpl.class );

private final TransactionCoordinator transactionCoordinator;
private final ExceptionConverter exceptionConverter;
private final JpaCompliance jpaCompliance;
private final AbstractSharedSessionContract session;

private TransactionDriver transactionDriverControl;

public TransactionImpl(
TransactionCoordinator transactionCoordinator,
ExceptionConverter exceptionConverter,
AbstractSharedSessionContract session) {
this.transactionCoordinator = transactionCoordinator;
this.exceptionConverter = exceptionConverter;
this.jpaCompliance = session.getFactory().getSessionFactoryOptions().getJpaCompliance();
this.session = session;

Expand Down Expand Up @@ -104,7 +101,7 @@ public void commit() {
internalGetTransactionDriverControl().commit();
}
catch (RuntimeException e) {
throw exceptionConverter.convertCommitException( e );
throw session.getExceptionConverter().convertCommitException( e );
}
}

Expand Down
Expand Up @@ -147,7 +147,8 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont

private Integer jdbcBatchSize;

protected transient ExceptionConverter exceptionConverter;
//Lazily initialized
private transient ExceptionConverter exceptionConverter;

private CriteriaCompiler criteriaCompiler;

Expand Down Expand Up @@ -217,7 +218,6 @@ public AbstractSharedSessionContract(SessionFactoryImpl factory, SessionCreation
.getService( TransactionCoordinatorBuilder.class )
.buildTransactionCoordinator( jdbcCoordinator, this );
}
exceptionConverter = new ExceptionConverterImpl( this );
}

protected void addSharedSessionTransactionObserver(TransactionCoordinator transactionCoordinator) {
Expand Down Expand Up @@ -314,7 +314,7 @@ public void close() {
}
catch ( HibernateException e ) {
if ( getFactory().getSessionFactoryOptions().isJpaBootstrap() ) {
throw this.exceptionConverter.convert( e );
throw getExceptionConverter().convert( e );
}
else {
throw e;
Expand Down Expand Up @@ -436,7 +436,6 @@ public Transaction accessTransaction() {
if ( this.currentHibernateTransaction == null ) {
this.currentHibernateTransaction = new TransactionImpl(
getTransactionCoordinator(),
getExceptionConverter(),
this
);
}
Expand Down Expand Up @@ -557,7 +556,7 @@ public <T> T execute(final LobCreationContext.Callback<T> callback) {
return callback.executeOnConnection( connection );
}
catch (SQLException e) {
throw exceptionConverter.convert(
throw getExceptionConverter().convert(
e,
"Error creating contextual LOB : " + e.getMessage()
);
Expand Down Expand Up @@ -644,7 +643,7 @@ public QueryImplementor getNamedQuery(String name) {
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) {
Expand Down Expand Up @@ -737,7 +736,7 @@ public QueryImplementor createQuery(String queryString) {
}
catch (RuntimeException e) {
markForRollbackOnly();
throw exceptionConverter.convert( e );
throw getExceptionConverter().convert( e );
}
}

Expand All @@ -757,7 +756,7 @@ public <T> QueryImplementor<T> createQuery(CriteriaQuery<T> criteriaQuery) {
return (QueryImplementor<T>) criteriaCompiler().compile( (CompilableCriteria) criteriaQuery );
}
catch ( RuntimeException e ) {
throw exceptionConverter.convert( e );
throw getExceptionConverter().convert( e );
}
}

Expand All @@ -768,7 +767,7 @@ public QueryImplementor createQuery(CriteriaUpdate criteriaUpdate) {
return criteriaCompiler().compile( (CompilableCriteria) criteriaUpdate );
}
catch ( RuntimeException e ) {
throw exceptionConverter.convert( e );
throw getExceptionConverter().convert( e );
}
}

Expand All @@ -779,7 +778,7 @@ public QueryImplementor createQuery(CriteriaDelete criteriaDelete) {
return criteriaCompiler().compile( (CompilableCriteria) criteriaDelete );
}
catch ( RuntimeException e ) {
throw exceptionConverter.convert( e );
throw getExceptionConverter().convert( e );
}
}

Expand Down Expand Up @@ -812,7 +811,7 @@ public <T> QueryImplementor<T> createQuery(
return query;
}
catch ( RuntimeException e ) {
throw exceptionConverter.convert( e );
throw getExceptionConverter().convert( e );
}
}

Expand All @@ -833,7 +832,7 @@ public <T> QueryImplementor<T> createQuery(String queryString, Class<T> resultCl
return query;
}
catch ( RuntimeException e ) {
throw exceptionConverter.convert( e );
throw getExceptionConverter().convert( e );
}
}

Expand Down Expand Up @@ -911,7 +910,7 @@ protected <T> QueryImplementor<T> buildQueryFromName(String name, Class<T> resu
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) {
throw !( e instanceof IllegalArgumentException ) ? new IllegalArgumentException( e ) : e;
Expand Down Expand Up @@ -1029,7 +1028,7 @@ public NativeQueryImplementor createNativeQuery(String sqlString, Class resultCl
return query;
}
catch ( RuntimeException he ) {
throw exceptionConverter.convert( he );
throw getExceptionConverter().convert( he );
}
}

Expand All @@ -1054,7 +1053,7 @@ public NativeQueryImplementor createNativeQuery(String sqlString, String resultS
return query;
}
catch ( RuntimeException he ) {
throw exceptionConverter.convert( he );
throw getExceptionConverter().convert( he );
}
}

Expand All @@ -1069,7 +1068,7 @@ public NativeQueryImplementor getNamedNativeQuery(String name) {
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
Expand All @@ -1095,7 +1094,7 @@ protected NativeQueryImplementor getNativeQueryImplementor(
return query;
}
catch ( RuntimeException he ) {
throw exceptionConverter.convert( he );
throw getExceptionConverter().convert( he );
}
}

Expand Down Expand Up @@ -1161,7 +1160,10 @@ public ScrollableResultsImplementor scroll(NativeSQLQuerySpecification spec, Que
}

@Override
public ExceptionConverter getExceptionConverter(){
public ExceptionConverter getExceptionConverter() {
if ( exceptionConverter == null ) {
exceptionConverter = new ExceptionConverterImpl( this );
}
return exceptionConverter;
}

Expand Down Expand Up @@ -1234,8 +1236,7 @@ private void readObject(ObjectInputStream ois) throws IOException, ClassNotFound
.buildTransactionCoordinator( jdbcCoordinator, this );

entityNameResolver = new CoordinatingEntityNameResolver( factory, interceptor );
exceptionConverter = new ExceptionConverterImpl( this );
this.disallowOutOfTransactionUpdateOperations = !getFactory().getSessionFactoryOptions().isAllowOutOfTransactionUpdateOperations();

}

}

0 comments on commit 8c22824

Please sign in to comment.