简体   繁体   English

外部事务中获取的实体是托管还是分离?

[英]Are entities fetched outside transaction managed or detached?

I always thought that entities fetched before a transaction (before entityManager.getTransaction().begin() ) were detached from the persistence context (while using the same entity manager to fetch entities as to handle the transaction). 我一直认为,在事务处理之前(在entityManager.getTransaction().begin() )获取的实体与持久性上下文分离(同时使用相同的实体管理器来获取处理事务的实体)。 In fact, every time I was managing transactions, I had to explicitly merge entities that were fetched outside of it. 实际上,每次我管理事务时,我都必须显式合并从其外部获取的实体。 If I didn't, updates were not reflected in the database. 如果没有,更新将不会反映在数据库中。

But recently, I had a discussion with a colleague who experienced this situation the opposite way. 但是最近,我和一位经历相反情况的同事进行了讨论。 He had to explicitly detach every entities fetched before transaction to avoid updating database. 他必须明确分离事务之前获取的每个实体,以避免更新数据库。

The only difference between our codes is that my persistence.xml file is version 2.0 and his is version 1.0. 我们的代码之间的唯一区别是,我的persistence.xml文件是2.0版,而他的文件是1.0版。 But he is using JPA 2.1 in his code. 但是他在代码中使用的是JPA 2.1。 Is that the reason for this strange behavior or is there something I'm missing here? 是这种奇怪行为的原因,还是我在这里缺少什么?

We both use Hibernate as implementation. 我们都使用Hibernate作为实现。

PersistenceContext may have two scopes: PersistenceContext可能有两个作用域:

  • TRANSACTIONAL-SCOPE 交易范围
  • EXTENDED-SCOPE 扩展范围

TRANSACTIONAL-SCOPE 交易范围

Occurs only when EntityManager is container-managed. 仅在EntityManager由容器管理时发生。 In this case, current PersistenceContext is active as long as current transaction exists. 在这种情况下,只要存在当前事务,就可以激活当前PersistenceContext。

EXTENDED-SCOPE 扩展范围

Occurs when EntityManager is application-managed (be default) or container-managed (EntityManager needs annotation: @PersistenceContext( type=PersistenceContextType.EXTENDED) ). 在EntityManager由应用程序管理(默认)或容器管理(EntityManager需要注释: @PersistenceContext( type=PersistenceContextType.EXTENDED) )时发生。

Long story short: EXTENDED-SCOPE means that current PersistenceContext lives as long as your bean lives. 长话短说:EXTENDED-SCOPE表示当前的PersistenceContext生存时间与您的bean生存时间一样长。

If and how things merge automatically into a transactional context depends on several factors. 事物是否以及如何自动融合到交易环境中取决于几个因素。 The JPA implementation in use, the persistence context scope, and also the transaction isolation in use. 使用的JPA实现,持久性上下文范围以及使用的事务隔离。 Without seeing the whole environment this cannot easily be answered. 如果不看整个环境,就很难回答。 I would assume your application allows non transactional reads (dirty read, phantom read, non-repeatable read) and hence you are "allowed" to decide wether you want to merge. 假设您的应用程序允许非事务性读取(脏读取,幻像读取,不可重复读取),因此您被“允许”决定是否要合并。 Another explanation could be some probem with your application code mixing several persistence contexts and hence requiring you to merge your entities read in one persistence context into another persitence context (the one which you started your TX on). 另一个解释可能是您的应用程序代码混合了多个持久性上下文,因此需要您将在一个持久性上下文中读取的实体合并到另一个持久性上下文(开始TX的上下文)中。

They will be managed. 他们将得到管理。 Any changes will be reflected to the DB within next flush . 任何更改将在下一次flush反映到数据库。 Transaction commits are flushed right away, along with the changes enqueued by pre-transactional actions. 事务提交以及事务前操作排队的更改会立即被刷新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM