[英]Spring EntityManager Commit Transaction as method completes
I am using spring EntityManager
and have a requirement to commit records on method completion. 我正在使用spring
EntityManager
并且需要在方法完成时提交记录。 That is i have two methods for ex :: 那就是我有两种方法用于ex ::
@Override
@Transactional
public void upsert(String lastSuccessfullRun) {
for( tableData in Tables){
insertIntoDB(tableData);
}
}
and the method insertIntoDB contains the buisness logic which actually does the update queries 并且方法insertIntoDB包含实际上执行更新查询的业务逻辑
@Override
@Transactional(propagation=Propagation.REQUIRES_NEW)
public void insertIntoDB (String tableData) {
em.persist(tableData)
}
But the problem is that the method doesn't commit as it returns for the next loop in upsert method. 但是问题是该方法没有提交,因为它返回upsert方法中的下一个循环。
How can i commit on method completion ? 如何完成方法?
Check the documentation . 检查文档 。
Method visibility and @Transactional
方法可见性和@Transactional
When using proxies, you should apply the @Transactional annotation only to methods with public visibility.
使用代理时,应仅将@Transactional注释应用于具有公共可见性的方法。 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.
如果使用@Transactional注释对受保护的,私有的或程序包可见的方法进行注释,则不会引发任何错误,但是带注释的方法不会显示已配置的事务设置。 Consider the use of AspectJ (see below) if you need to annotate non-public methods.
如果需要注释非公共方法,请考虑使用AspectJ(请参见下文)。
Even if your method is public, you're calling it in another method in the same class, so you are not going trugh the proxy and the @Transactional(propagation=Propagation.REQUIRES_NEW)
on insertIntoDB
method has no effect. 即使您的方法是公共的,也要在同一类的另一个方法中调用它,所以您不会使用代理,并且
insertIntoDB
方法上的@Transactional(propagation=Propagation.REQUIRES_NEW)
无效。
So try it in AspectJ mode, as in the docs. 因此,请像文档一样在AspectJ模式下尝试。
try using as shown below : 尝试如下所示使用:
@Override
@Transactional(propagation=Propagation.REQUIRES_NEW)
public void upsert(String lastSuccessfullRun) {
for( tableData in Tables){
insertIntoDB(tableData);
}
}
@Override
@Transactional(propagation=Propagation.SUPPORTS)
public void insertIntoDB (String tableData) {
em.persist(tableData)
}
if you use "propagation=Propagation.REQUIRES_NEW" in insertIntoDB method then it will create a new transaction and commit it when insertIntoDB method completed . 如果在insertIntoDB方法中使用“ propagation = Propagation.REQUIRES_NEW”,则它将创建一个新事务,并在insertIntoDB方法完成时提交该事务。
@Transactional only works when you call the method throw proxy so in your case it doesn't work. @Transactional仅在调用方法throw proxy时起作用,因此在您的情况下不起作用。 If you really want that your changes reflected in Database and in your PersistenceContext that associated with your active transaction you can use
如果您确实希望您的更改反映在与活动事务相关联的数据库和PersistenceContext中,则可以使用
EntityManager.flush();
But keep it in mind when using flush() the changes to the data are reflected in database after encountering flush, but it is still in transaction and can be rollback and the transaction is still active but when you use commit() the changes are made into the database & transaction also ends there. 但是请记住,使用flush()时,遇到刷新后,对数据的更改会反映在数据库中,但是它仍处于事务中并且可以回滚并且事务仍处于活动状态,但是当您使用commit()时,会进行更改进入数据库,交易也就此结束。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.