Skip to content

Instantly share code, notes, and snippets.

@jeffbicca
Last active August 29, 2015 14:08
Show Gist options
  • Save jeffbicca/7b84d448f805f7d7b9bc to your computer and use it in GitHub Desktop.
Save jeffbicca/7b84d448f805f7d7b9bc to your computer and use it in GitHub Desktop.
An EntityManagerProducer without memory leaks and stuff that works with JBoss EAP 6.1
package pckg;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Default;
import javax.enterprise.inject.Disposes;
import javax.enterprise.inject.Produces;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
@ApplicationScoped
public class EntityManagerProducer {
@PersistenceUnit(unitName="unit-ds")
EntityManagerFactory factory;
@Produces
@Default
@RequestScoped
public EntityManager createEntityManager() {
EntityManager em = factory.createEntityManager();
return em;
}
public void dispose(@Disposes @Default EntityManager em) {
if(em.isOpen()){
em.close();
}
em = null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment