我在grails中有一个抽象服务类,用于设置事务属性。
像这样:
abstract class AbstractService {
static transactional = false
}
class MyService extends AbstractService {
// methods
}
仅通过扩展
MyService
,AbstractService
中的方法是否也会事务性错误。
最佳答案
简而言之,可以,但是只要您不定义
import grails.transaction.Transactional
import org.springframework.transaction.interceptor.TransactionAspectSupport
class TestingService extends TestService {
//DO NOT ENABLE THIS OTHERWISE IT IS TRANSACTIONAL
//static transactional = true
def doIt() {
def aa = TransactionAspectSupport?.currentTransactionInfo()
return aa
}
@Transactional
def doIt2() {
def aa = TransactionAspectSupport?.currentTransactionInfo()
return aa
}
}
因此,如果您覆盖静态事务,那么它将变为事务。如果您在扩展抽象类中坚持使用静态声明,那么即使您定义@Transactional注释,现在也是如此。结果将为空。对于下面的测试。
如果不支持事务,aa将返回null,如果是,您将看到类似
PROPAGATION_REQUIRED,ISOLATION_DEFAULT
的信息
关于spring - 静态事务属性会传播到子类吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34397283/