【问题标题】:Get the data which is inserted using @Transactional in the same transaction获取在同一事务中使用@Transactional 插入的数据
【发布时间】:2016-07-08 01:23:30
【问题描述】:

我在一种方法中插入数据(有@Transactional(propagation = Propagation.Required)),但在另一种方法中(有@Transactional(propagation = Propagation.Required))如果我尝试获取相同的数据,它会给出null。

这两个方法都写在服务层@Transactional (rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)

如何获取插入到同一个事务中的数据。 类似的东西:-

 @Service
    public class Service{
        @Transactional
        public void method(){
            mapper.insert();        //insert to DB(Using Mapper interface)
            ServiceLayer.method2()
        }
    }

@Service
public void ServiceLayer{

    @Transactional
    public static void method2(){
        result  = mapper.select() //Select inserted data - returning null
    }
}

【问题讨论】:

    标签: java spring spring-mvc spring-mybatis


    【解决方案1】:

    为了保持对当前会话所做的更改,您可以调用entityManager.flush();

    【讨论】:

    • 怎么样??我正在使用 spring-mybatis。
    【解决方案2】:

    它可能有效,但不是解决方案。 在您的情况下,来自Service.method()Transaction 创建了一个尚未提交的事务。这就是你无法获取它的原因。

    【讨论】:

      【解决方案3】:

      我找到了答案...从ServiceLayer.method2() 中删除@transactional 后,它工作正常。

      【讨论】:

        猜你喜欢
        • 2021-08-05
        • 2012-11-20
        • 1970-01-01
        • 2013-08-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多