简体   繁体   English

方法完成时,Spring EntityManager提交事务

[英]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.

相关问题 使用 Java Spring 和 Hibernate 和 EntityManager 的 @Transactional 方法中的事务提交问题 - Problem with transaction commit in @Transactional methods using Java Spring and Hibernate and EntityManager 没有使用Spring / EclipseLink的EntityManager事务 - No EntityManager transaction with Spring/EclipseLink 如何强制EntityManager的提交事务 - How to force commit transaction of EntityManager Spring&JUnit Transaction:@after方法中的事务清理导致部分提交 - Spring & JUnit Transaction: transaction cleanup in @after method causing partial commit 为什么在春季交易中EntityManager为null? - why EntityManager is null in spring transaction? 使用Spring手动交易管理获取EntityManager - Get EntityManager using spring manual transaction management 如何在 Spring 中的共享 EntityManager 上手动启动事务? - How to manually start a transaction on a shared EntityManager in Spring? 持久/提交后,EntityManager连接停留在“事务中的空闲”状态 - EntityManager connections stuck at 'Idle in transaction' after persist/commit Spring hibernate,如何在事务提交或事务回滚后调用一些方法 - Spring hibernate , how to call some method after transaction commit or transaction rollback Spring - Transaction应该在一个方法中提交,但是应该在执行DB事务的其他方法中回滚 - Spring - Transaction should commit in one method but should rollback in other methods which does a DB transaction
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM