简体   繁体   English

Grails-事务性Spring noRollbackFor不起作用

[英]Grails - Transactional Spring noRollbackFor not working

I'm using a grails 1.3.7 and have the following code: 我正在使用grails 1.3.7,并具有以下代码:

Grails service: Grails服务:

class MyClass {
   static transactional = true

  @Transactional(noRollbackFor = MyException.class)
  public MyObject myMethod(Map map1, Boolean bl1 = false) throws MyException {
   //codes
     if(...){
        throw new MyException("msg")
     }
}

MyException: MyException:

class MyException extends Exception{

 def errors = []

MyException(errors){
    super(errors.toString())
    this.errors = errors
}

} }

When code throws an MyException, I catch the following error: Transaction rolled back because it has been marked as rollback-only 当代码引发MyException时,我捕获以下错误: 事务回滚,因为它已被标记为仅回滚

Ps. 附言 If I change static transactional = true the error not occurs. 如果我更改为static transactional = true,则不会发生错误。

Any Idea? 任何想法?

I you use annotations, you should set 如果您使用注释,则应设置

static transactional=false

ie invalidate grails' transactional proxy, so that there is no overlap with the proxy from spring AOP 例如,使grails的事务代理无效,从而使春季AOP中的代理没有重叠

This should work: 这应该工作:

@Transactional(noRollbackFor=[FooException, BarException])
def doSomething(...) {
     ...
}

But remember, if you use transactional annotations, grails automatic transactions does not work in the service where you place that it. 但是请记住,如果您使用事务注释,则grails自动事务在您放置它的服务中将不起作用。 You need to set: 您需要设置:

@Transactional
class myService(...) {
     static transactional = false
     ...
}

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

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