簡體   English   中英

@Transactional傳播私人方法

[英]@Transactional propagation on private methods

我有以下代碼:

@Service
public class MyService implements IMyService {
    @Inject
    IAnotherService anotherService;
    // injects go here
    // some code
    @Transactional(isolation=Isolation.SERIALIZABLE)
    public Result myMethod() {
        // stuff done here
        return this.myPrivateMethod()
    } 

    private Result myPrivateMethod() {
         // stuff done here
         // multiple DAO SAVE of anObject
         anotherService.processSomething(anObject);
         return result; 
    }
}

@Service
public class AnotherService implements IAnotherService {
      // injections here
      // other stuff

      @Transactional(isolation=SERIALIZABLE)
      public Result processSomething(Object anObject) {
         // some code here
         // multiple dao save
         // manipulation of anObject
         dao.save(anObject);
      }
}
  1. @Transactional行為是否傳播到myPrivateMethod,即使它是私有的?
  2. 如果在processSomething()上發生Runtime Exception ,並且從myPrivateMethod調用processSomethingmyPrivateMethodmyMethod會回滾嗎?
  3. 如果對1和2的答案為否,那么如何在不創建另一個@Service情況下實現這一目標? 如何在@Transactional上下文中的公共服務方法中提取方法並調用多個私有方法?
  4. isolation=Isolation.SERIALIZABLE選項是synchronized方法的一個很好的替代方案嗎?

我知道這已經回答了,但我仍然有疑慮。

  1. 如果從注釋@Transactional的公共方法調用myPrivateMethod,則會傳播它。
  2. 如果第一個條件為TRUE是,它將回滾。
  3. 比較數據庫的隔離級別和類方法的同步是錯誤的。 根本不應該對它們進行比較。 如果您的方法將在多線程環境中使用,則應該同步方法(在某些情況下,請注意,使用線程安全代碼是不夠的)。 隔離級別SERIALIZABLE用於數據庫級別。 它是最嚴格的隔離級別,它可以在您運行某些查詢之前鎖定大量表,以幫助您的數據不會轉變為某種不一致的狀態。 您應確保需要此級別的隔離,因為這可能會導致性能問題。 所以答案是否定的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM