在 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)的限制。
添加回答
舉報
0/150
提交
取消