在Java EE应用程序中处理多个EntityManager [英] Handle multiple EntityManager in Java EE application

查看:377
本文介绍了在Java EE应用程序中处理多个EntityManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有大约10个EntityManagers的Java EE应用程序(EM的数量可能会增加)。我的应用程序还包含许多无状态,有状态和消息驱动的bean。

I have Java EE application with about 10 EntityManagers (number of EMs will probably increase). My application also contains many stateless, statefull and message driven beans.

而不是在每个bean中使用 @PersistenceContext (以及2种方法来检测用户使用哪个EM),我可能将所有这些存储在单例bean中并使用其他bean访问它。就像这样,不用担心可维护性。

Rather than inject in each bean my EMs with @PersistenceContext (and 2 methods to detect which EM to use for user), I probably store all of that inside a singleton bean and access it with others beans. Like that, no worries about maintainability.

然而,将EM存储在一个单独的bean中是否可以线程安全?可以出现瓶颈吗?

Nevertheless, is it thread-safe to store EMs inside one singleton bean? Can a bottleneck appear?

另一个解决方案是创建一个抽象类,所有bean都会扩展它。

Another solution is to create an abstract class and all beans will extend it.

什么是更好的解决方案?

What is the better solution?

推荐答案

容器管理的实体管理器会自动传播当前的JTA事务和 EntityManager 映射到同一持久性单元的引用提供对该事务中持久性上下文的访问。因此,除了并发问题之外,从单例共享实体管理器并不是一个好习惯,这会导致为您在bean上调用的每个方法使用相同的事务上下文。

一个简单的解决方案来满足您的需求是在bean中注入 EntityManagerFactory 引用并创建 EntityManager 对象调用 createEntityManager()方法。
缺点是您应该手动管理事务,不再依赖容器。

否则,另一种方法可能是将所有实体管理器注入主企业bean并在服务中实现业务逻辑bean使用您传递适当管理器的方法。
后一种解决方案的一个例子:

Container managed entity managers are automatically propagated with the current JTA transaction and EntityManager references that are mapped to the same persistence unit provide access to the persistence context within that transaction. So it's not good practice to share an entity manager from a singleton, apart from concurrency problems, it would result in using the same transaction context for every method you call on your beans.
A simple solution to your need is to inject EntityManagerFactory references in your beans and create EntityManager objects calling the createEntityManager() method. The drawback is that you should manage transactions manually, no more relying on the container.
Otherwise another approach could be inject all of your entity managers in a main enterprise bean and implement business logic in service beans with methods to which you pass the appropriate managers. An example of the latter solution:

@Stateless
class MainBean {
    @PersistenceContext EntityManager em1;
    @PersistenceContext EntityManager em2;
    ...
    @EJB WorkerBean1 workerBean1;
    @EJB WorkerBean2 workerBean2;
    ...
    void method1(Object param1, Object param2) {
        workerBean1.method1(em1, param1, param2);
    }

    void method2(Object param1, Object param2, Object param3) {
        workerBean2.method2(em2, param1, param2, param3);
    }
    ...
}

@Stateless
class WorkerBean1 {
    void method1(EntityManager em, Object param1, Object param2) {
        ...
    }
    ...
}

@Stateless
class WorkerBean2 {
    void method2(EntityManager em, Object param1, Object param2, Object param3) {
        ...
    }
    ...
}

这篇关于在Java EE应用程序中处理多个EntityManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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