Background initialization for Hibernate SessionFactory breaks transaction management [SPR-14379] #18952
SpringSessionContext.currentSession() invokes TransactionSynchronizationManager.getResource(this.sessionFactory) and TransactionSynchronizationManager maintains a Map of resource references keyed by object references. Ordinarily this is not a problem when invoking TransactionSynchronizationManager.getResource(this.sessionFactory) since the SessionFactory reference is always the same.
However, when background initialization of the SessionFactory is enabled (new feature in 4.3 GA as per #18305) then the SessionFactory can turn out to be a proxy created using a LocalSessionFactoryBuilder$BootstrapSessionFactoryInvocationHandler depending on how the SessionFactory reference was obtained. Thus the map key for TransactionSynchronizationManager will be different. So effectively this will cause SpringSessionContext.currentSession() to return different Hibernate SessionImpl instances within the same thread and transaction context. Meaning all hell breaks loose when this happens.
TransactionSynchronizationManager.getResource() invokes TransactionSynchronizationUtils.unwrapResourceIfNecessary(key) before attempting to use the key as provided. This method's name seems to imply that it would solve the proxy wrapping issue described above, however it does not. It only checks for InfrastructureProxy and ScopedProxyUnwrapper, but neither resolves the issue described here.
The workaround is not to use background initilization as per #18305 (i.e. avoiding configuration of a bootstrapExecutor for LocalSessionFactoryBean). The solution would probably involve either fixing TransactionSynchronizationUtils.unwrapResourceIfNecessary() or having the LocalSessionFactoryBuilder$BootstrapSessionFactoryInvocationHandler proxy implement InfrastructureProxy so that the correct SessionFactory reference could be unwrapped.
Affects: 4.3 GA
Referenced from: commits 01f1158
The text was updated successfully, but these errors were encountered:
Juergen Hoeller commented
The exposed bootstrap proxy implements the