Skip to content

Background initialization for Hibernate SessionFactory breaks transaction management [SPR-14379] #18952

Closed

Description

@spring-projects-issues

Manuel Dominguez Sarmiento opened SPR-14379 and commented

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

Issue Links:

Referenced from: commits 01f1158

Activity

  1. spring-projects-issues commented on Jun 20, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    The exposed bootstrap proxy implements the InfrastructureProxy interface now, unwrapping the underlying SessionFactory. This should address the problem that you've outlined for scenarios where the native SessionFactory is being accessed directly in some places.

  2. spring-projects-issues commented on Jun 20, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Manuel Dominguez Sarmiento commented

    Thanks Juergen.

  3. added
    type: bug
    in: data
    on Jan 11, 2019
  4. added this to the 4.3.1 milestone on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: datatype: bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions