diff options
| author | Stefan Suhren <suhren.stefan@fh-swf.de> | 2015-11-23 14:42:32 +0100 |
|---|---|---|
| committer | Stefan Suhren <suhren.stefan@fh-swf.de> | 2015-11-23 15:57:23 +0100 |
| commit | 723c66ea036b5bc53ddeee0005e335cffff16a45 (patch) | |
| tree | 019505d47b785b7dd0a8a4bed671ba9ca17fa569 /src/jpa | |
| parent | 25ae0a23d15383df7f84ad51ee8f078c519ed963 (diff) | |
| download | JCatalog-723c66ea036b5bc53ddeee0005e335cffff16a45.tar.gz JCatalog-723c66ea036b5bc53ddeee0005e335cffff16a45.zip | |
Use singleton for EntityManagerFactory
Diffstat (limited to 'src/jpa')
| -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; + } +} |
