package org.jpwh.web.dao;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
/*
This CDI annotation declares that only one producer is needed in the
whole application, there will only ever be one instance of
EntityManagerProducer
.
*/
@javax.enterprise.context.ApplicationScoped
public class EntityManagerProducer {
/*
The Java EE runtime will give you the persistence unit
configured in your persistence.xml
, which is also an
application-scoped component. (If you use CDI standalone and outside
a Java EE environment, you can instead use the static
Persistence.createEntityManagerFactory()
bootstrap.)
*/
@PersistenceUnit
private EntityManagerFactory entityManagerFactory;
/*
Whenever an EntityManager
is needed, the create()
method is called. The container will re-use the same EntityManager
during a request handled by our server. (If you forget the
@RequestScoped
annotation on the method, the
EntityManager
would be application-scoped like the producer class!)
*/
@javax.enterprise.inject.Produces
@javax.enterprise.context.RequestScoped
public EntityManager create() {
return entityManagerFactory.createEntityManager();
}
/*
When a request is over and the request context is being destroyed, the CDI
container will call this method to get rid of an EntityManager
instance. Since you created this application-managed persistence context
(see ), it's your job to close it.
*/
public void dispose(@javax.enterprise.inject.Disposes
EntityManager entityManager) {
if (entityManager.isOpen())
entityManager.close();
}
}