java - Spring EntityManager 在方法完成时提交事务

标签 java spring entitymanager propagation

我正在使用 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/

相关文章:

java - 如何将hadoop序列文件的值更改为jackson解析器?

Java Graphics2D - 绘制具有渐变不透明度的图像

java - 预检响应具有无效的 HTTP 状态代码 401 - Spring

oracle - 如何查看 ORACLE 中当前设置的 IDLE_TIME?

java - GoogleAppEngine : ClassNotFoundException : javax. jdo.metadata.ComponentMetadata

jsf-2 - 带有 CDI 托管 Bean 和有状态 session Bean 的 LazyInitializationException

glassfish 中的 Java 堆空间错误

Java如何限制作用于方法的线程数量

java - 为什么我的 Java servlet 过滤器不能通过 HTTPS 工作?

java - 现在我正在使用 Maven 的 Spring 项目错误