Does jboss handle managed Entity Manager concurrency issues for me?

半腔热情 提交于 2019-12-04 17:04:19

The container is responsible for scanning for @PersistenceContext annotations and injection of EntityManagers. It can proxy the instances of EntityManager.

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:

Keep in mind that the entity manager is not expected to be thread safe (don't inject it into a servlet class variable which is visible to multiple threads).

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!