Last active
August 29, 2015 14:08
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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