私有方法上的@Transactional 传播

dan*_*rba 7 java spring transactions isolation

我有以下代码:

@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);
      }
}
Run Code Online (Sandbox Code Playgroud)
  1. @Transactional行为是否会传播到 myPrivateMethod 即使它是私有的?。
  2. 如果 aRuntime Exception发生在processSomething(),并processSomething从 调用myPrivateMethod,将myPrivateMethodmyMethod执行回滚?。
  3. 如果 1 和 2 的答案是否定的,我怎样才能在不必创建另一个的情况下实现这一目标@Service?如何在@Transactional上下文中的公共服务方法中提取方法并调用多个私有方法?
  4. isolation=Isolation.SERIALIZABLE选择一个很好的替代synchronized方法?

我知道这已经得到了回答,但我仍然有疑问。

mom*_*ilo 7

  1. 如果从注释为 @Transactional 的公共方法调用 myPrivateMethod ,则传播它。
  2. 如果第一个条件为 TRUE 是,它将回滚。
  3. 比较数据库的隔离级别和类方法的同步是一种误解。他们根本不应该被比较。如果您的方法将在多线程环境中使用,您应该同步方法(请注意,在某些情况下,拥有线程安全代码是不够的)。隔离级别 SERIALIZABLE 用于数据库级别。它是限制性最强的隔离级别,它可以在您运行某个查询时锁定很多表,在它完成之前,帮助您的数据不会变成某种不一致的状态。您应该确保您需要这种级别的隔离,因为这可能会导致性能问题。所以答案是否定的。