@Transactional Annotation +用于循环中的数据插入 [英] @Transactional Annotation + for a data insertion in a loop

查看:816
本文介绍了@Transactional Annotation +用于循环中的数据插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为 CMS 应用程序使用 Spring 3,JPA + Hibernate 。在那个应用程序中,我有一个服务类方法,该方法使用 @Transactional 注释与 rollBack 属性进行注释。在该方法中,我使用循环将数据(即实体类)插入到表中。对于循环实体类的每个迭代都必须保存到数据库中。但它没有发生。提交仅在循环的执行完成并退出该方法时发生。然后它立即提交并保存。但是在这种情况下,在提交数据之前,我需要读取数据。我尝试使用 ISOLATION LEVEL 读取未提交的内容,但由于使用了默认的 JPADialect ,所以不支持。还尝试添加 jpaDialect hibernate 实现,但仍然无效。请帮助解决此问题的解决方法。还有一点,有没有什么方法可以使用传播所需的方法。 解决方案

使用循环删除方法上的事务注释。 / p>

在循环中调用一个单独的方法来执行保存,使该方法成为事务性的


I am using Spring 3, JPA + Hibernate for a CMS application. In that application I have a service class method which is annotated with @Transactional Annotation with rollBack property. Inside that method I am inserting data (ie entity classes) to a table using a loop. For each iteration of the loop entity classes has to be saved to the database. But it is not happening. The commit only happens when the execution of the loop has completed and exits from the method. Then it commits and saves all at once. But I need to read data once it gets inserted into the database before committing in this case. I tried with the ISOLATION LEVEL to read uncommitted but it didn't supported since I am using the default JPADialect. Also tried to add the hibernate implementation of jpaDialect but still it didn't worked. Please help with a workaround for this problem. One more thing, is there any way using propagation required method.

解决方案

remove the transactional annoation on the the method with loop.

In the loop call a separate method to perform the save, make that method transactional

这篇关于@Transactional Annotation +用于循环中的数据插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆