Details
Description
When using Apache Aries (container managed Transactions, JTA) with Hibernate for persistence, if we start multiple threads to read from the database without transactions (Transaction Annotation is ommited), we get the following stacktrace:
org.hibernate.AssertionFailure: possible non-threadsafe access to the session
at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:130)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:1103)
at org.hibernate.loader.Loader.processResultSet(Loader.java:960)
at org.hibernate.loader.Loader.doQuery(Loader.java:910)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:341)
at org.hibernate.loader.Loader.doList(Loader.java:2516)
at org.hibernate.loader.Loader.doList(Loader.java:2502)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2332)
at org.hibernate.loader.Loader.list(Loader.java:2327)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:490)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:355)
at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:195)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1247)
at org.hibernate.internal.QueryImpl.list(QueryImpl.java:101)
The reason we got this exception in a non-transacted mode is that we inject EntityManager in our implementation and use it for different requests from multiple threads. Unfortunately, the EntityManager is not thread safe and we have to refactor the code to inject EntityManagerFactory instead since EntityManagerFactory is thread safe and then create EntityManagers in each method programmatically.
From Aries stand point, we should be able to improve it in order to ease the pain by creating a synchronized entity manager, or a proxy which creates entity managers for each request. And the proxy would probably do the following:
1. create EntityManager;
2. perform request;
3. close EntityManager;
Attachments
Issue Links
- relates to
-
ARIES-970 EntityManager service factory
- Resolved