本文介绍了应该何时创建/打开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:

我了解到 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 areheavyweight objects. Each factorymight maintain a metadata cache,object state cache, EntityManagerpool, connection pool, and more. Ifyour application no longer needs anEntityManagerFactory, you shouldclose it to free these resources.

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

      When an EntityManagerFactory closes,all EntityManagers from that factory,and by extension all entities managedby those EntityManagers, becomeinvalid.

      为了反复创建和关闭新的
      工厂,保持工厂长时间开盘

      更好。因此,大多数应用程序
      将永远不会关闭工厂,或者当应用程序退出
      时,只需
      关闭它。

      It is much better to keep a factoryopen for a long period of time thanto repeatedly create and close newfactories. Thus, most applicationswill never close the factory, or onlyclose it when the application isexiting.

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

      Only applications that requiremultiple factories with differentconfigurations have an obvious reasonto create and close multipleEntityManagerFactory instances.

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

      Only one EntityManagerFactory ispermitted to be created for eachdeployed persistence unitconfiguration. Any number ofEntityManager instances may becreated from a given factory.

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

09-27 08:24