我有一个类有两个这样的持久单元
@Stateless
public class SearchRepository {
@PersistenceContext(unitName = "XYZ")
private EntityManager entityManager;
@PersistenceContext(unitName = "ABC")
private EntityManager entityManager1;
@Transactional(value = TxType.REQUIRED)
public void doSomething(){
entityManager.persist(someentity);
entityManager1.remove(someentity);
}
}
我有这段代码并且工作正常,任何人都可以解释一下如何在这种类型的代码中管理事务。 JPA 如何管理具有多个持久性单元的事务 我无法猜测事务在这里是如何工作的。任何帮助将不胜感激。提前致谢。
最佳答案
@Transactional
绑定(bind)到您的默认 PlatformTransactionManager
-Bean - 例如实体管理器
。使用 entityManager1
可能会以非事务方式与此 PersistenceContext 交互。
[可以使用属性transactionManager
更改@Transactional
的默认行为。]
关于java - 事务如何与多个持久性单元一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42805327/