Does Spring support PersistenceContextType.EXTENDED?
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 toPersistenceContextType.TRANSACTION
. This default is what you need to receive a sharedEntityManager
proxy. The alternative,PersistenceContextType.EXTENDED
, is a completely different affair: This results in a so-called extendedEntityManager
, which is not thread-safe and hence must not be used in a concurrently accessed component such as a Spring-managed singleton bean. ExtendedEntityManagers
are only supposed to be used in stateful components that, for example, reside in a session, with the lifecycle of theEntityManager
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).
精彩评论