如何在@Transactional处于课堂级别时使用readonly [英] How to use readonly when @Transactional is on class level
问题描述
服务
类中调用 DAO $ @Transactional
c $ c>方法
我想知道如何使用只读特定服务方法。
我必须定义额外的 @Transactional $ c $在这个服务方法中使用 readonly = true
,或者有另外一种方法? > 从Spring API 此处。
当评估方法的事务设置时,派生最多的位置优先。在以下示例中,DefaultFooService类在类级别使用只读事务的设置进行注释,但同一类中updateFoo(Foo)方法的@Transactional注释优先于定义的事务设置
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $'
public Foo getFoo(String fooName){
//做某事
}
//这些设置优先于此方法
@Transactional(readOnly = false,propagation = Propagation.REQUIRES_NEW)
public void updateFoo(Foo foo){
//做某事
}
}
$ @Transactional注解是元数据,它指定接口,类或方法必须具有事务性语义;例如,在调用此方法时启动全新的只读事务,暂停任何现有事务。默认的@Transactional设置如下:
传播设置为PROPAGATION_REQUIRED。
隔离级别为ISOLATION_DEFAULT。
/ b>
交易是读/写的。
交易超时默认为底层交易系统的默认超时,
任何RuntimeException都会触发回滚,而任何检查过的异常都不会。
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 method.
Do I have to define extra @Transactional
with readonly=true
on this service method, or there's another way?
解决方案 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.
这篇关于如何在@Transactional处于课堂级别时使用readonly的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!