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.
# 1 楼答案
Spring有一个带有静态工厂方法的特殊类,用于创建扩展实体管理器。见ExtendedEntityManagerCreator
此外,如果您注入一个
EntityManagerFactory
并调用createEntityManager(SynchronizationType.SYNCHRONIZED)
,它将创建具有扩展持久性上下文的实体管理器(在提交事务后,它不会自动分离实体)# 2 楼答案
在Implementing DAOs based on plain JPA中有一个简短的讨论:
所以不,听起来不像是弹簧支撑着它们
# 3 楼答案
是的,春天支持它。例如,参见Injection of PersistenceContext with PersistenceContextType EXTENDED