亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Spring Boot 中的同步事務

Spring Boot 中的同步事務

冉冉說 2021-11-11 18:11:27
在 Springboot 中,我調用它的每個服務都會打開一個事務,當服務返回時它會關閉該連接,但在我的情況下,我需要創建一個將同步運行的方法(此方法僅在非同步方法中運行),他需要OPEN 和 CLOSE 事務獨立于是否打開一個事務,并且該方法中的每個 SQL 操作僅在該方法引發錯誤時才會回滾。如果調用它的方法拋出錯誤,他將不會回滾同步方法所做的任何事情。所以我嘗試使用這個示例:@Servicepublic class MyService {    @Autowired    private MyRepository myRepository;    public void methodNotSyncronized(String arg1, String arg2){        logger.debug("init method no syncronied");        MyObjct myObj = myRepository.findOne(1);        methodSyncronized(arg2);        myRepository.save(myObj);   //If I got some error here everything that methodSyncronized did should remaining        logger.debug("finish method no syncronied");    }    @Transactional(isolation = Isolation.SERIALIZABLE, propagation = Propagation.REQUIRES_NEW)    private synchronized  String methodSyncronized(String arg){        logger.debug("init method  syncronied");        //Here I will insert or delete something        }}但是當我調試這段代碼時,我得到了:o.h.e.t.internal.TransactionImpl         : begin                       myService         : init method no syncronied                       myService         : init method  syncronied                       myService         : finish method no syncronied                     o.h.e.t.internal.TransactionImpl         : committing我怎樣才能解決這個問題另一件事,即使我只對休眠打印的數字求和,我調用的每項服務也是如此:o.h.e.t.internal.TransactionImpl         : begin                       o.h.e.t.internal.TransactionImpl         : committing即使我把 @Transactional(readOnly=true)放在方法中
查看完整描述

1 回答

?
慕田峪7331174

TA貢獻1828條經驗 獲得超13個贊

它不起作用,因為Spring @Transaction method call by the method within the same class, does not work:這是 Spring AOP(動態對象和 cglib)的限制。



查看完整回答
反對 回復 2021-11-11
  • 1 回答
  • 0 關注
  • 279 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號