JPA Web application management strategies
We're currently developing a J2EE web app using JPA as our data access layer. We're currently entertaining a couple different strat开发者_如何学JAVAegies to utilizing caching within our applications.
-
Create an
EntityManager
per request- Get caching within the request scope, but lose the cache at the end of the request
- Makes it easy to rollback any changes from an entire request
-
Create an
EntityManager
per Http Session- Get caching within the session scope
- Makes things more complicated with transactions, would have to create a new transaction per request, then would be able to rollback changes per request
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?
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.
What server are you using? It should be able to inject an EntityManger for you rather than requiring the application deal with it.
精彩评论