Reputation: 2118
We're currently developing a J2EE web app using JPA as our data access layer. We're currently entertaining a couple different strategies to utilizing caching within our applications.
EntityManager
per request
EntityManager
per Http Session
I've seen both strategies in the Hibernate documentation. My question is which is more widely used and if we do the EntityManager
per session, will we run into synchronization issues by using the same EntityManager
across different request threads?
Upvotes: 3
Views: 1477
Reputation: 597016
The more widely used approach is to create an EntityManager
per request. However this should remain hidden from you. You should use some dependency-injection mechanism (spring/CDI/EJB) that will inject the proper EntityManager
where @PersistenceContext
is placed.
If you are interested how this is achieved in the common case where your beans are some sort singletons (one stateless bean / one spring bean of scope singleton) - the container actually injects a proxy in the target object. And each time the proxy is consulted, it gets the current EntityManager
instance, which is (in the case of spring at least) bound to a ThreadLocal
(= request in this case)
Update: If you want to implement this functionality in a home-grown framework, then use cglib / javassist / JDK proxy and inject it where @PersistenceContext
is placed. A request = thread. On each request that needs data access, create a new EntityManager
and store it in a ThreadLocal
. Remember to clean it in the end because servlet containers reuse threads. From the proxy you can obtain the current ThreadLocal
value.
But in case you are not far into the project, then I'd suggest moving to something more stable, like spring, cdi or guice.
Upvotes: 3
Reputation: 6317
What server are you using? It should be able to inject an EntityManger for you rather than requiring the application deal with it.
Upvotes: 0