jboss处理管理的Entity Manager并发问题吗? [英] Does jboss handle managed Entity Manager concurrency issues for me?
问题描述
似乎实体管理器实例jboss管理和提供是绑定到持久化上下文的实际实现的代理。
It seems that the Entity Manager instance jboss manages and provides is a proxy to the actual implementation bound to a persistence context.
这个实际实现收集提供的隔离JTA事务(每个事务上下文)。
This actual implementation gathers the isolation provided by JTA transactions (per transaction contexts).
这让我认为我不需要担心处理代理实例时的并发问题。
That makes me think I don't need to worry about concurrency issues when dealing with the proxy instance.
也许我甚至可以缓存这个代理实例,如果我决定从JNDI查找而不是容器注入?
Maybe I can even cache this proxy instance if I decide to bring it from JNDI lookups instead of container injection?
这是合理吗?
推荐答案
容器负责扫描 @PersistenceContext
注释和注入 EntityManagers
。 可以代理 EntityManager
的实例。
The container is responsible for scanning for @PersistenceContext
annotations and injection of EntityManagers
. It can proxy the instances of EntityManager
.
在EJB中,容器负责依赖注入,你可以肯定你是线程安全的。
In EJB, where the container is responsible for dependency injection, you can be sure that you're thread-safe. The persistence context will be shared between multiple components within the same transaction.
但是,如果你注入这个 EntityManager
使用 @PersistenceContext
在Servlets环境(其中并发是一个问题),你不线程安全。您应该使用 @PersistenceUnit
。您可以参考 JBoss 7 JPA参考指南的这一部分:
However, if you inject this EntityManager
using @PersistenceContext
in Servlets environment (where the concurrency is a concern), you're not thread-safe. You should use @PersistenceUnit
instead. You can refer to this part of the JBoss 7 JPA Reference Guide:
请记住,实体管理器不应该是线程安全的
into a servlet class variable which is visible to
multiple threads)。
总结了我所知道的关于持久化上下文共享的JTA事务和代理 EntityManager
由容器,并发布这里。我希望你会发现它有用。
Some time ago I've summed up what I know about Persistence Context sharing between JTA transactions and proxying of EntityManagers
by the container and published it here. I hope you'll find it useful.
这篇关于jboss处理管理的Entity Manager并发问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!