diff options
Diffstat (limited to 'src/jpa/EntityManagerFactorySingleton.java')
| -rw-r--r-- | src/jpa/EntityManagerFactorySingleton.java | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/jpa/EntityManagerFactorySingleton.java b/src/jpa/EntityManagerFactorySingleton.java new file mode 100644 index 0000000..dbd4873 --- /dev/null +++ b/src/jpa/EntityManagerFactorySingleton.java @@ -0,0 +1,54 @@ +/** + * + */ +package jpa; + +import javax.annotation.PreDestroy; +import javax.enterprise.context.ApplicationScoped; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; + +/** + * A singleton for application wide {@link EntityManagerFactory}. Increases + * performance significant. + * + * @author Stefan Suhren + * @version 1.0 + */ +@ApplicationScoped +public class EntityManagerFactorySingleton +{ + private static final String DB_PU = "catalog"; + + private static final EntityManagerFactorySingleton singleton = new EntityManagerFactorySingleton(); + + private EntityManagerFactory emf; + + private EntityManagerFactorySingleton() + { + } + + public static EntityManagerFactorySingleton getInstance() + { + return singleton; + } + + public EntityManagerFactory getEntityManagerFactory() + { + if (emf == null) + { + emf = Persistence.createEntityManagerFactory(DB_PU); + } + return emf; + } + + @PreDestroy + public void closeEmf() + { + if (emf != null && emf.isOpen()) + { + emf.close(); + } + emf = null; + } +} |
