获取 EntityManagerFactory 的最佳实践

发布于 2024-12-11 12:47:27 字数 245 浏览 0 评论 0原文

在 Web 应用程序(jsp/servlet)中获取 EntityManagerFactory 的最佳方法是什么? 这是一个好方法何时创建/打开 EntityManagerFactory 实例? , 还是从 JNDI 或其他地方获取它更好?

What is the best approach to get EntityManagerFactory in web app(jsp/servlets)?
Is this a good way When should EntityManagerFactory instance be created/opened?,
or is it better to get it from JNDI, or something else?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

夏九 2024-12-18 12:47:28

它们是重量级的,并且应该在应用程序范围内。因此,您需要在应用程序启动时打开它们,并在应用程序关闭时关闭它们。

如何执行此操作取决于您的目标容器。它支持 EJB 3.x(Glassfish、JBoss AS 等)吗?如果是这样,那么如果您只是使用 @PersistenceContext 通常的方式:

@Stateless
public class FooService {

    @PersistenceContext
    private EntityManager em;

    public Foo find(Long id) {
        return em.find(Foo.class, id);
    }

    // ...
}

如果您的目标容器不支持 EJB(例如 Tomcat、Jetty 等)并且由于某种原因,像 OpenEJB 这样的 EJB 附加组件也不是一个选项,因此您需要手动创建EntityManager(和事务)你自己,那么你最好的选择是ServletContextListener。下面是一个基本启动示例:

@WebListener
public class EMF implements ServletContextListener {

    private static EntityManagerFactory emf;

    @Override
    public void contextInitialized(ServletContextEvent event) {
        emf = Persistence.createEntityManagerFactory("unitname");
    }

    @Override
    public void contextDestroyed(ServletContextEvent event) {
        emf.close();
    }

    public static EntityManager createEntityManager() {
        if (emf == null) {
            throw new IllegalStateException("Context is not initialized yet.");
        }

        return emf.createEntityManager();
    }

}

(注意:在 Servlet 3.0 之前,此类需要通过 web.xml 中的 注册code> 而不是 @WebListener

可以用作:

EntityManager em = EMF.createEntityManager();
// ...

They're heavyweight and they're supposed to be in the application scope. So, you need to open them on application startup and close them on application shutdown.

How to do that depends on your target container. Does it support EJB 3.x (Glassfish, JBoss AS, etc)? If so, then you don't need to worry about opening/closing them (neither about transactions) at all if you just do the JPA job in EJBs with @PersistenceContext the usual way:

@Stateless
public class FooService {

    @PersistenceContext
    private EntityManager em;

    public Foo find(Long id) {
        return em.find(Foo.class, id);
    }

    // ...
}

If your target container doesn't support EJBs (e.g. Tomcat, Jetty, etc) and an EJB add-on like OpenEJB is also not an option for some reason, and you're thus manually fiddling with creating EntityManagers (and transactions) yourself, then your best bet is a ServletContextListener. Here's a basic kickoff example:

@WebListener
public class EMF implements ServletContextListener {

    private static EntityManagerFactory emf;

    @Override
    public void contextInitialized(ServletContextEvent event) {
        emf = Persistence.createEntityManagerFactory("unitname");
    }

    @Override
    public void contextDestroyed(ServletContextEvent event) {
        emf.close();
    }

    public static EntityManager createEntityManager() {
        if (emf == null) {
            throw new IllegalStateException("Context is not initialized yet.");
        }

        return emf.createEntityManager();
    }

}

(note: before Servlet 3.0, this class needs to be registered by <listener> in web.xml instead of @WebListener)

Which can be used as:

EntityManager em = EMF.createEntityManager();
// ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文