Created
September 12, 2017 14:18
-
-
Save chongma/0eb583c08b711eebcb9e4a5c533c94e8 to your computer and use it in GitHub Desktop.
This file contains 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 uk.me.kissy.sales.producers; | |
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 = "sales") | |
private EntityManagerFactory entityManagerFactory; | |
@Produces | |
@Default | |
@RequestScoped | |
public EntityManager create() { | |
return this.entityManagerFactory.createEntityManager(); | |
} | |
public void dispose(@Disposes @Default EntityManager entityManager) { | |
if (entityManager.isOpen()) { | |
entityManager.close(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment