PersistenceContext can get cleared before load completes using StatelessSessionImpl

Description

When an association gets loaded eagerly by {{StatelessSessionImpl#internalLoad}} calling {{StatelessSessionImpl#get}}, {{temporaryPersistenceContext}} may get cleared before the load is complete. If this happens, Hibernate will not find an expected {{EntityEntry}}, and {{TwoPhaseLoad#afterInitialize}} will throw a {{NullPointerException}}.

The test case for https://hibernate.atlassian.net/browse/HHH-13633 caused the {{NullPointerException}} because there was a bug that caused a lazy association to be loaded eagerly. Once that bug was fixed by HHH-13633, the problem was no longer reproduced by that test case.

Modifying that test case to make the association eager reproduced this issue again.


Original description:

A test case added for https://hibernate.atlassian.net/browse/HHH-13633#icft=HHH-13633 throws NullPointerException.

found that this commit for https://hibernate.atlassian.net/browse/HHH-12425#icft=HHH-12425 caused the NPE.

Activity

Show:

Former userNovember 7, 2019 at 6:01 AM

Fixed in 5.3 branch as well.

Former userOctober 17, 2019 at 5:27 PM

Fixed in master.

Andrea BorieroSeptember 20, 2019 at 3:59 PM

withhttps://hibernate.atlassian.net/browse/HHH-12425 the Loader postpones the call to TwoPhaseLoad#afterInitialize().

What happens with a is that Loader#initilizeEntitiesAndCollection:

  • First calls TwoPhaseLoad#initilizeEntity() that triggers a call to StatelessSessionImpl#get()

public Object get(String entityName, Serializable id, LockMode lockMode) { .... if ( temporaryPersistenceContext.isLoadFinished() ) { temporaryPersistenceContext.clear(); } return result; }

that clears the PersistenceContext

  • Then calls{{TwoPhaseLoad#afterInitialize()}}

public static void afterInitialize( final Object entity, final SharedSessionContractImplementor session) { ... final EntityEntry entityEntry = persistenceContext.getEntry( entity ); entityEntry.getPersister().afterInitialize( entity, session ); }

but the PersistenceContext has been previously cleared so entityEntry is null and the call to entityEntry.getPersister() causes a NPE.

Former userSeptember 20, 2019 at 6:27 AM

This issue does not affect 5.3 because https://hibernate.atlassian.net/browse/HHH-12425 was not backported to 5.3.

Fixed

Details

Assignee

Reporter

Fix versions

Affects versions

Priority

Created September 20, 2019 at 6:25 AM
Updated November 7, 2019 at 6:04 AM
Resolved November 7, 2019 at 6:01 AM

Flag notifications