简体   繁体   English

如何将多个JPA EntityManager保留/合并到单个事务中

[英]How to wrap multiple JPA EntityManager persist/merge into single transaction

I need to update several tables in a single transaction. 我需要在一个事务中更新几个表。

Currently, I have the following structure in my code: 当前,我的代码具有以下结构:

(pseudo-code) (伪代码)

process(updateList, mainObject);  // returns list of objects and main business object to persist/update
persistTable1(mainObject);  // table 1

int objectIndex = 0;
while(objectIndex < updateList.size()) {
    persistTable2(updateList[objectIndex]); // table 2
    objectIndex++;
}

Question: How do I wrap the persistTable1 (which will always get called once - no matter what), and the persistTable2 call (which will get called 0 to n times) into a single transaction, so that if any persist/merge fails then everything will get rolled-back? 问题:如何将persistTable1(总是调用一次,无论如何)和persistTable2(将调用0到n次)包装到单个事务中,这样,如果任何持久/合并失败,则一切会回滚吗?

The persist/update code is using JPA EntityManager. 持久性/更新代码正在使用JPA EntityManager。

You can use the transactional context in the method caller. 您可以在方法调用器中使用事务上下文。

@Transactional(propagation = Propagation.REQUIRES_NEW) @Transactional(传播=传播。REQUIRES_NEW)

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

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