Skip to content

Instantly share code, notes, and snippets.

@chongma
Created September 12, 2017 14:18
Show Gist options
  • Save chongma/0eb583c08b711eebcb9e4a5c533c94e8 to your computer and use it in GitHub Desktop.
Save chongma/0eb583c08b711eebcb9e4a5c533c94e8 to your computer and use it in GitHub Desktop.
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