何时应创建/打开 EntityManagerFactory 实例?

发布于 2024-10-09 01:45:50 字数 2079 浏览 5 评论 0原文

好吧,我读了很多关于如何在单例中编写实体管理器工厂的文章/示例。

其中一个对我来说最容易理解一点:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忆依然 2024-10-16 01:45:50
  • 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.

  • More than one entity manager factory
    instance may be available
    simultaneously in the JVM. Methods of the EntityManagerFactory
    interface are threadsafe.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文