My immediate thought would be to leverage Session's notion of auto
closing after transaction
(org.hibernate.cfg.Settings#isAutoCloseSessionEnabled). That is all
the Synchronization in EntityManagerImpl is doing.
Those hooks are already in place, maybe just add a way for
EntityManager to tell Session it should be auto closed. Currently that
is set up front via a boolean (SessionImpl#autoCloseSessionEnabled).
We would instead need a delegation there. Or maybe just pass
discardOnClose as autoCloseSessionEnabled if the JPA transaction type
is JTA; if no transaction is active when EM.close() EM.close would
still close the Session immediately (that code is there already), but
thats ok because there would be no synchronization registered with the
actual JTA platform.
Not sure about Search.
On Wed 25 Apr 2012 01:41:39 AM CDT, Gail Badner wrote:
Scott has verified that the failure he is seeing involves 2
synchronizations. The first is from EntityManagerImpl and the second is HSearch.
The problem is that the EntityManagerImpl (anonymous) Synchronization.afterCompletion()
closes the session, which in turn, calls TransactionCoordinatorImpl.close() which calls
reset(), which clears the synchronizations from the registry. After that finishes,
SynchronizationRegistryImpl.notifySynchronizationsAfterTransactionCompletion() attempts to
execute HSearch afterCompletion() synchronization, but the registry has already been
cleared, resulting in the ConcurrentModificationException.
I initially thought a possible fix would be to defer calling
TransactionCoordinatorImpl.reset() if the afterCompletion() synchronizations are being
executed. Doing this would be fairly straightforward.
The problem is that, since the EntityManager afterCompletion() synchronization is
executed first, the session would be closed before the HSearch afterCompletion()
synchronization is executed. I mentioned this to Sanne and he said that the HSearch
afterCompletion() synchronization might need access to the persistence context to, for
example, initialize a collection, or to get entity state. Obviously it can't if the
session is already closed.
Is there a reason why the EntityManager synchronization is registered before the HSearch
synchronization?
Is there a way to always make the "session-closing" synchronization last?
Steve, do you see a better way around this?
Thanks,
Gail
_______________________________________________
hibernate-dev mailing list
hibernate-dev(a)lists.jboss.org
https://lists.jboss.org/mailman/listinfo/hibernate-dev