私有方法上的@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. 如果 Runtime Exception 发生在 processSomething() 上,并且 processSomething 是从 myPrivateMethod 调用的,myPrivateMethodmyMethod 会回滚吗? .
  3. 如果对 1 和 2 的回答是否定的,我怎样才能在不创建另一个 @Service 的情况下实现它?如何在 @Transactional 上下文中提取方法并在 public 服务方法中调用多个私有方法?
  4. isolation=Isolation.SERIALIZABLE 选项是 synchronized 方法的一个很好的替代方法吗?

我知道这个问题已经得到解答,但我仍然有疑问。

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