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.
The proposed fix is to make CacheSynchronization check TransactionFactory.Context#isClosed().
StatelessSessionImpl.isFlushBeforeCompletionEnabled() could return a boolean based on whether or not it was closed, rather than CacheSynchronization checking for closedness itself.
Hibernate Core 3.5.6, Oracle v10.2.0.3.0, JDBC driver v188.8.131.52.0, WebSphere Application Server 184.108.40.206
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.
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!