如何在JPA中共享EntityManagerFactory [英] How to share EntityManagerFactory in jpa

查看:120
本文介绍了如何在JPA中共享EntityManagerFactory的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是jpa的新手,这是场景...我正在开发Web应用程序...

I am Newbie to jpa Here is the scenario...I am developing a web Apllication...where

多个用户可以登录...

multiple users can login...

当user1注销...我正在使用以下代码...

when the user1 LogOut...i am using below code...

public static void closeEntityManagerFactory() {

    if (!entityManager.getTransaction().isActive()){

    entityManager.getTransaction().begin();

    }

    entityManager.close();

    emf.close();

}

即使user2已登录,它也会抛出错误,告诉您Entity Manager已关闭....

Even if the user2 is loged in it throws error telling Entity Manager is closed....The

问题是一旦应用程序启动,我们不应该关闭EntityManager ... ???或者我们

question is we should not close the EntityManager once the application is up...??? Or we

应使用多个实例...如果是,如何实现此目标...帮助将是可观的:):)

should use Multiple instance...if yes how to achive this...help will be appreciable :) :)

推荐答案

EntityManagerFactory实例的创建成本很高,而EntityManager实例的创建成本并不高. 在Java SE Web应用程序中,大多数情况只能用一个EntityManagerFactory实例处理.

The EntityManagerFactory instance is expensive to create, the EntityManager ones are not. In a Java SE web application, most cases can be handled with only one EntityManagerFactory instance.

您可以在contextInitialized方法中初始化EntityManagerFactory. > ServletContextListener 并将实例存储在上下文属性(context.setAttribute(key, emf))中:

You can initialize the EntityManagerFactory in the contextInitialized method of a ServletContextListener and store the instance in a context attribute (context.setAttribute(key, emf)):

public class CustomServletContextListener implements ServletContextListener {

    private EntityManagerFactory entityManagerFactory;

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext context = sce.getServletContext();

        entityManagerFactory = Persistence.createEntityManagerFactory("UnitName");
        context.setAttribute("someKey", entityManagerFactory);
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        /* Some method that cleanly destroys
         * your entity manager factory */
        closeEntityManagerFactory();
    }
}

稍后,您可以使用context.getAttribute("someKey")检索EntityManagerFactory实例,并在需要时创建EntityManager实例.

Later, you can retrieve the EntityManagerFactory instance using context.getAttribute("someKey") and create EntityManager instance where needed.

最后,为了使ServerContextListener实现起作用,只需在web.xml中注册它即可:

Finally, in order to get your ServerContextListener implementation to work, simply register it in your web.xml:

<listener>
        <listener-class>com.company.listeners.CustomServletContextListener</listener-class>
</listener>

注意:是否需要短期或长期的EntityManager实例取决于应用程序的范围.在典型的Web应用程序中,跨越请求的短暂应用程序更为明智.有关与EntityManager实例有关的不同策略,请参阅Bauer和King,Manning的 Hibernate的Java持久性.文本同时处理了Hibernate和JPA.

Note: whether you need short-lived or long-lived EntityManager instances depends on the scopes of your application. In typical web-applications, short-lived ones that span the requests are more sensible. Refer to Java Persistence with Hibernate by Bauer and King, Manning for different strategies regarding EntityManager instances. The text addresses both Hibernate and JPA in parallel.

这篇关于如何在JPA中共享EntityManagerFactory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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