我正在使用 spring EntityManager
并需要在方法完成时提交记录。也就是说,我有两种方法::
@Override
@Transactional
public void upsert(String lastSuccessfullRun) {
for( tableData in Tables){
insertIntoDB(tableData);
}
}
方法 insertIntoDB 包含实际执行更新查询的业务逻辑
@Override
@Transactional(propagation=Propagation.REQUIRES_NEW)
public void insertIntoDB (String tableData) {
em.persist(tableData)
}
但问题是该方法不提交,因为它返回 upsert 方法中的下一个循环。
我如何提交方法完成?
最佳答案
检查documentation .
Method visibility and @Transactional
When using proxies, you should apply the @Transactional annotation only to methods with public visibility. If you do annotate protected, private or package-visible methods with the @Transactional annotation, no error is raised, but the annotated method does not exhibit the configured transactional settings. Consider the use of AspectJ (see below) if you need to annotate non-public methods.
即使您的方法是公共(public)的,您也会在同一类中的另一个方法中调用它,因此您不会通过代理和 @Transactional(propagation=Propagation.REQUIRES_NEW)
insertIntoDB
方法无效。
因此请在 AspectJ 模式下尝试,如文档中所示。
关于java - Spring EntityManager 在方法完成时提交事务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38720705/