blob: dbd4873da4177cf45ae72fb6daecf6d4b7806c61 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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;
}
}
|