How to use readonly when @Transactional is on class level

前端 未结 1 830
名媛妹妹
名媛妹妹 2021-01-22 09:35

I am using @Transactional on my service class which call DAO methods and I was wondering how can I use read only on specific service metho

相关标签:
1条回答
  • 2021-01-22 10:02

    From the Spring API here.

    The most derived location takes precedence when evaluating the transactional settings for a method. In the case of the following example, the DefaultFooService class is annotated at the class level with the settings for a read-only transaction, but the @Transactional annotation on the updateFoo(Foo) method in the same class takes precedence over the transactional settings defined at the class level.

    @Transactional(readOnly = true)
    public class DefaultFooService implements FooService {
    
      public Foo getFoo(String fooName) {
        // do something
      }
    
      // these settings have precedence for this method
      @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
      public void updateFoo(Foo foo) {
        // do something
      }
    }
    

    The @Transactional annotation is metadata that specifies that an interface, class, or method must have transactional semantics; for example, “start a brand new read-only transaction when this method is invoked, suspending any existing transaction”. The default @Transactional settings are as follows:

    Propagation setting is PROPAGATION_REQUIRED.

    Isolation level is ISOLATION_DEFAULT.

    Transaction is read/write.

    Transaction timeout defaults to the default timeout of the underlying transaction system, or to none if timeouts are not supported.

    Any RuntimeException triggers rollback, and any checked Exception does not.

    0 讨论(0)
提交回复
热议问题