【问题标题】:Spring Data JPA - How to have multiple transaction boundaries with same TransactionManagerSpring Data JPA - 如何使用相同的 TransactionManager 拥有多个事务边界
【发布时间】:2015-11-25 01:21:40
【问题描述】:

我有一项服务必须按顺序执行以下步骤。

1) insert database records
2) commit
3) call external service (This service need to see the inserts in step 1)
4) more inserts
5) commit

目前外部服务无法看到插入的行。 请建议如何在外部调用之前进行提交。我正在使用 Spring JPA/Hibernate。

谢谢

【问题讨论】:

    标签: spring jpa


    【解决方案1】:

    您需要确保两个操作都在各自的事务中运行,并且在 T2 执行之前提交 T1。您还需要注意此讨论:

    Spring @Transaction method call by the method within the same class, does not work?

    鉴于上述情况,这样的事情应该可以工作:

    @Service
    public class ClientService{
    
        @Autowired
        private RecordsService recordsService;
    
        @Autowired 
        private ExternalService externalService;
    
        public void insert(){
            recordsService.insertRecords();
            externalService.insertRecords();
        }
    }
    
    @Service
    public class RecordsService{
    
        @Transactional
        public void insertRecords(){
    
        }
    }
    
    @Service
    public class ExternalService{
    
        @Transactional
        public void insertRecords(){
    
        }
    }
    

    【讨论】:

    • 感谢艾伦的及时回复。欣赏它。
    猜你喜欢
    • 2016-06-05
    • 2013-02-15
    • 2013-08-26
    • 1970-01-01
    • 2015-08-18
    • 2015-05-04
    • 1970-01-01
    • 1970-01-01
    • 2014-08-21
    相关资源
    最近更新 更多