应该何时创建/打开EntityManagerFactory实例? [英] When should EntityManagerFactory instance be created/opened?

查看:146
本文介绍了应该何时创建/打开EntityManagerFactory实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我阅读了一些文章/示例如何以单身形式编写实体经理工厂。

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

我了解到 EntityManagerFactory(EMF)应该只创建一次在应用范围内。

并确保在使用后关闭EMF(?)

And also make sure to close the EMF once it's used (?)

所以我为商业方法编写了EMF助手类:

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

我使用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必须单身这么糟糕?我读到了并发问题,但由于我没有多线程语法经验,我对这个想法并不是很清楚。

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.

推荐答案


  • EntityManagerFactory实例是
    重量级对象。每个工厂
    可能维护元数据缓存,
    对象状态缓存,EntityManager
    池,连接池等。如果
    您的应用程序不再需要
    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.

      当EntityManagerFactory关闭,
      来自该工厂的所有EntityManagers,
      和扩展名由EntityManagers管理
      的所有实体,变为
      无效。

      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.

      只有需要
      多个工厂且具有不同
      配置的应用程序才有明显的原因
      来创建和关闭多个
      EntityManagerFactory实例。

      Only applications that require multiple factories with different configurations have an obvious reason to create and close multiple EntityManagerFactory instances.

      只允许为每个
      部署的持久性单元
      配置创建一个EntityManagerFactory
      。任意数量的
      EntityManager实例可能是从给定工厂创建的

      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.

      这篇关于应该何时创建/打开EntityManagerFactory实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆