何时应创建/打开 EntityManagerFactory 实例?
好吧,我读了很多关于如何在单例中编写实体管理器工厂的文章/示例。
其中一个对我来说最容易理解一点:
http://javanotepad.blogspot.com /2007/05/jpa-entitymanagerfactory-in-web.html
我了解到 EntityManagerFactory (EMF) 最好只在应用程序范围内创建一次。
并且还要确保关闭 EMF一旦使用(?),
所以我编写了 EMF 帮助程序类供业务方法使用:
public class EmProvider {
private static final String DB_PU = "KogaAlphaPU";
public static final boolean DEBUG = true;
private static final EmProvider singleton = new EmProvider();
private EntityManagerFactory emf;
private EmProvider() {}
public static EmProvider getInstance() {
return singleton;
}
public EntityManagerFactory getEntityManagerFactory() {
if(emf == null) {
emf = Persistence.createEntityManagerFactory(DB_PU);
}
if(DEBUG) {
System.out.println("factory created on: " + new Date());
}
return emf;
}
public void closeEmf() {
if(emf.isOpen() || emf != null) {
emf.close();
}
emf = null;
if(DEBUG) {
System.out.println("EMF closed at: " + new Date());
}
}
}//end class
我的方法使用 EmProvider:
public String foo() {
EntityManager em = null;
List<Object[]> out = null;
try {
em = EmProvider.getInstance().getEntityManagerFactory().createEntityManager();
Query query = em.createNativeQuery(JPQL_JOIN); //just some random query
out = query.getResultList();
}
catch(Exception e) {
//handle error....
}
finally {
if(em != null) {
em.close(); //make sure to close EntityManager
}
//should I not close the EMF itself here?????
EmProvider.getInstance().closeEmf();
}
我确保按照建议在方法级别关闭 EntityManager (em)。但是EntityManagerFactory什么时候应该关闭?为什么EMF必须是单例的这么糟糕???我读过有关并发问题的文章,但由于我没有多线程语法经验,因此我无法真正清楚这个想法。
Ok, I read bunch of articles/examples how to write Entity Manager Factory in singleton.
One of them easiest for me to understand a bit:
http://javanotepad.blogspot.com/2007/05/jpa-entitymanagerfactory-in-web.html
I learned that EntityManagerFactory (EMF) should only be created once preferably in application scope.
And also make sure to close the EMF once it's used (?)
So I wrote EMF helper class for business methods to use:
public class EmProvider {
private static final String DB_PU = "KogaAlphaPU";
public static final boolean DEBUG = true;
private static final EmProvider singleton = new EmProvider();
private EntityManagerFactory emf;
private EmProvider() {}
public static EmProvider getInstance() {
return singleton;
}
public EntityManagerFactory getEntityManagerFactory() {
if(emf == null) {
emf = Persistence.createEntityManagerFactory(DB_PU);
}
if(DEBUG) {
System.out.println("factory created on: " + new Date());
}
return emf;
}
public void closeEmf() {
if(emf.isOpen() || emf != null) {
emf.close();
}
emf = null;
if(DEBUG) {
System.out.println("EMF closed at: " + new Date());
}
}
}//end class
And my method using EmProvider:
public String foo() {
EntityManager em = null;
List<Object[]> out = null;
try {
em = EmProvider.getInstance().getEntityManagerFactory().createEntityManager();
Query query = em.createNativeQuery(JPQL_JOIN); //just some random query
out = query.getResultList();
}
catch(Exception e) {
//handle error....
}
finally {
if(em != null) {
em.close(); //make sure to close EntityManager
}
//should I not close the EMF itself here?????
EmProvider.getInstance().closeEmf();
}
I made sure to close EntityManager (em) within method level as suggested. But when should EntityManagerFactory be closed then? And why EMF has to be singleton so bad??? I read about concurrency issues but as I am not experienced multi-thread-grammer, I can't really be clear on this idea.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
EntityManagerFactory 实例是
重量级的物体。各工厂
可能会维护元数据缓存,
对象状态缓存,EntityManager
池、连接池等等。如果
您的应用程序不再需要
EntityManagerFactory,你应该
关闭它以释放这些资源。
当 EntityManagerFactory 关闭时,
该工厂的所有 EntityManager,
并通过扩展管理所有实体
通过那些EntityManagers,成为
无效。
保留工厂要好得多
开放时间长于
重复创建和关闭新的
工厂。因此,大多数应用
永远不会关闭工厂,或者只会
当应用程序运行时关闭它
退出。
仅适用于需要
多家工厂不同
配置有一个明显的原因
创建和关闭多个
EntityManagerFactory 实例。
只有一个EntityManagerFactory
允许为每个创建
部署的持久性单元
配置。任意数量的
EntityManager 实例可能是
从给定工厂创建。
实例可能可用
同时在 JVM 中。 EntityManagerFactory 的方法
接口是线程安全的。
EntityManagerFactory instances are
heavyweight objects. Each factory
might maintain a metadata cache,
object state cache, EntityManager
pool, connection pool, and more. If
your application no longer needs an
EntityManagerFactory, you should
close it to free these resources.
When an EntityManagerFactory closes,
all EntityManagers from that factory,
and by extension all entities managed
by those EntityManagers, become
invalid.
It is much better to keep a factory
open for a long period of time than
to repeatedly create and close new
factories. Thus, most applications
will never close the factory, or only
close it when the application is
exiting.
Only applications that require
multiple factories with different
configurations have an obvious reason
to create and close multiple
EntityManagerFactory instances.
Only one EntityManagerFactory is
permitted to be created for each
deployed persistence unit
configuration. Any number of
EntityManager instances may be
created from a given factory.
instance may be available
simultaneously in the JVM. Methods of the EntityManagerFactory
interface are threadsafe.