简体   繁体   English

@Transactional传播私人方法

[英]@Transactional propagation on private methods

I have the following code: 我有以下代码:

@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. Does the @Transactional behavior propagate to myPrivateMethod even if it's private?. @Transactional行为是否传播到myPrivateMethod,即使它是私有的?
  2. If a Runtime Exception occurs on processSomething() , and processSomething is called from myPrivateMethod , will myPrivateMethod and myMethod do rollback?. 如果在processSomething()上发生Runtime Exception ,并且从myPrivateMethod调用processSomethingmyPrivateMethodmyMethod会回滚吗?
  3. If the answer to 1 and 2 is no, how can I achieve that without having to create another @Service ?. 如果对1和2的答案为否,那么如何在不创建另一个@Service情况下实现这一目标? How can I do extract method and invoke multiple private methods inside a public service method within a @Transactional context?. 如何在@Transactional上下文中的公共服务方法中提取方法并调用多个私有方法?
  4. Is isolation=Isolation.SERIALIZABLE option a good alternative to synchronized methods?. isolation=Isolation.SERIALIZABLE选项是synchronized方法的一个很好的替代方案吗?

I know this has been answered already but I'm still having doubts. 我知道这已经回答了,但我仍然有疑虑。

  1. If myPrivateMethod is called from public method that is annotated @Transactional it is propagated. 如果从注释@Transactional的公共方法调用myPrivateMethod,则会传播它。
  2. If first condition is TRUE yes it will roll-back. 如果第一个条件为TRUE是,它将回滚。
  3. It is misconception to compare isolation level on database and synchronization of class method. 比较数据库的隔离级别和类方法的同步是错误的。 They shouldn't be compared at all. 根本不应该对它们进行比较。 If your method will be used in multi-thread env you should synchronize method(be aware in some circumstances it is not enough to have thread safe code). 如果您的方法将在多线程环境中使用,则应该同步方法(在某些情况下,请注意,使用线程安全代码是不够的)。 Isolation level SERIALIZABLE is used on database level. 隔离级别SERIALIZABLE用于数据库级别。 It is most restrictive isolation level, and it can lock a lot of tables when you run some query, before it is is done, to help your data not turn to some inconsistent state. 它是最严格的隔离级别,它可以在您运行某些查询之前锁定大量表,以帮助您的数据不会转变为某种不一致的状态。 You should be sure that you need this level of isolation because this can lead to performance issues. 您应确保需要此级别的隔离,因为这可能会导致性能问题。 So answer is NO. 所以答案是否定的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM