开发者

Does Spring support PersistenceContextType.EXTENDED?

开发者 https://www.devze.com 2023-02-13 02:36 出处:网络
A @Stateful EJB can use Persist开发者_如何学运维enceContextType.EXTENDED for reusing the same EntityManager across multiple transactions. This is useful in implementing conversational scopes. Does Spr

A @Stateful EJB can use Persist开发者_如何学运维enceContextType.EXTENDED for reusing the same EntityManager across multiple transactions. This is useful in implementing conversational scopes. Does Spring have any support for this?


There is a short discussion of this in Implementing DAOs based on plain JPA:

The @PersistenceContext annotation has an optional attribute type, which defaults to PersistenceContextType.TRANSACTION. This default is what you need to receive a shared EntityManager proxy. The alternative, PersistenceContextType.EXTENDED, is a completely different affair: This results in a so-called extended EntityManager, which is not thread-safe and hence must not be used in a concurrently accessed component such as a Spring-managed singleton bean. Extended EntityManagers are only supposed to be used in stateful components that, for example, reside in a session, with the lifecycle of the EntityManager not tied to a current transaction but rather being completely up to the application.

So no, it doesn't sound like Spring supports them.


Yes, Spring supports it. See, for example, Injection of PersistenceContext with PersistenceContextType EXTENDED.


Spring has a special class with static factory methods for creation of extended entity managers. See ExtendedEntityManagerCreator

Also, if you inject an EntityManagerFactory and invoke createEntityManager(SynchronizationType.SYNCHRONIZED) it will create the entity manager with extended persistence context (it will not automatically detach entities after commit of transaction).

0

精彩评论

暂无评论...
验证码 换一张
取 消