JTA CacheSynchronization can cause exceptions with stateless sessions

Description

1. Background

In a JTA environment (using an XA data source), JDBCContext registers CacheSynchronization objects for running before & after transaction completion.

If a stateless session is opened within an existing session and using the same connection, a CacheSynchronization will be registered against the StatelessSessionImpl. However, org.hibernate.transaction.CacheSynchronization.beforeCompletion() is called when the transaction ends; not when the stateless session ends.

In pseudo-code this might look like, using Spring's @Transactional aspect:

org.hibernate.impl.StatelessSessionImpl.managedFlush() will error if the session has been closed. CacheSynchronization.beforeCompletion() will call managedFlush() if:

  • StatelessSessionImpl.isFlushModeNever() is false.

  • and StatelessSessionImpl.isFlushBeforeCompletionEnabled() is true

  • and the transaction isn't rollback-only.

As expected, StatelessSessionImpl has hardcoded false and true for isFlushModeNever() and isFlushBeforeCompletionEnabled(), respectively.

The above pseudo-code will therefore fail when the transaction is committed. The top of the stack-trace is:

Looking at github, this still affects CacheSynchronization in Hibernate 3.6.

2. Proposed fix

The proposed fix is to make CacheSynchronization check TransactionFactory.Context#isClosed().

From:

To:

2.1. Mockito-based test

2.2. Alternatives

StatelessSessionImpl.isFlushBeforeCompletionEnabled() could return a boolean based on whether or not it was closed, rather than CacheSynchronization checking for closedness itself.

Environment

Hibernate Core 3.5.6, Oracle v10.2.0.3.0, JDBC driver v11.2.0.2.0, WebSphere Application Server 7.0.0.13

Activity

Show:
Brett Meyer
April 7, 2014, 5:44 PM

In an effort to clean up, in bulk, tickets that are most likely out of date, we're transitioning all ORM 3 tickets to an "Awaiting Test Case" state. Please see http://in.relation.to/Bloggers/HibernateORMJIRAPoliciesAndCleanUpTactics for more information.

If this is still a legitimate bug in ORM 4, please provide either a test case that reproduces it or enough detail (entities, mappings, snippets, etc.) to show that it still fails on 4. If nothing is received within 3 months or so, we'll be automatically closing them.

Thank you!

Brett Meyer
July 8, 2014, 3:10 PM

Bulk rejecting stale issues. If this is still a legitimate issue on ORM 4, feel free to comment and attach a test case. I'll address responses case-by-case. Thanks!

Assignee

Unassigned

Reporter

Andrew Flegg

Fix versions

None

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

Priority

Major
Configure