jboss 是否为我处理托管实体管理器并发问题?
看来 jboss 管理和提供的实体管理器实例是绑定到持久性上下文的实际实现的代理。
该实际实现收集了 JTA 事务(每个事务上下文)提供的隔离。
这让我认为在处理代理实例时我不需要担心并发问题。
如果我决定从 JNDI 查找而不是容器注入中获取该代理实例,也许我什至可以缓存它?
这合理吗?
It seems that the Entity Manager instance jboss manages and provides is a proxy to the actual implementation bound to a persistence context.
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.
Maybe I can even cache this proxy instance if I decide to bring it from JNDI lookups instead of container injection?
Is that reasonable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
容器负责扫描
@PersistenceContext
注释并注入EntityManagers
。它可以代理EntityManager
的实例。在 EJB 中,容器负责依赖注入,您可以确保线程安全。持久性上下文将在同一事务内的多个组件之间共享。
但是,如果您在 Servlet 环境(需要考虑并发性)中使用
@PersistenceContext
注入此EntityManager
,则您不是线程安全的。您应该使用@PersistenceUnit
来代替。您可以参考JBoss 7 JPA参考指南的这一部分:前段时间,我总结了我对 JTA 事务之间的持久性上下文共享和容器代理 EntityManagers 的了解,并将其发布了 此处。我希望你会发现它很有用。
The container is responsible for scanning for
@PersistenceContext
annotations and injection ofEntityManagers
. It can proxy the instances ofEntityManager
.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.
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: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.