I need advice which strategy to use with lazy loading.
My project is based on Spring, Struts 2 and Hibernate JPA. I have enough OneToMany and ManyToMany relationships and it's reasonable to use lazy loading. It seems very convenient to load data in Struts Actions with OpenEntityManagerInViewFilter in such way like:
Code:
Style style = user.getStyle()
But to avoid LazyLoadException I have to use:
Code:
@PersistenceContext(type=PersistenceContextType.EXTENDED)
Unfortunately, EntityManager in this case becomes not thread-safe, according to Spring docs: "
Extended EntityManagers are not thread-safe, hence they must not be used in concurrently accessed beans (which Spring-managed singletons usually are)." EXTENDED means that EntityManager is shared in user session and my EntityManager is injected in singleton services.
I've found this issue when tried to make quick sequential page reloading which call some selects and got:
Code:
java.sql.SQLException: You can't operate on a closed Statement!!!
What's the best practice in this situation:
1. Try synchronize session access to EntityManager by myself or put each user database requests in separate queue (something terrible I suppose).
2. Use HQL queries with
join fetch to preload data for access outside of transaction (maybe overhead).
3. Or something else???
And why we need EXTENDED context if it's so unreliable??
Thanks for help!