Grails,抛出异常后保存域类

Jos*_*ron 7 grails grails-orm

使用grails 3.3.8

如果在发出保存的服务方法中抛出了捕获的异常,则不会保存任何domainclass.save(flush:true,failOnError:true).即

try {
    //some code that throws exception
} catch (Exception exception) {
    print 'some message'
}

domainclass.save(flush:true,failOnError:true)
Run Code Online (Sandbox Code Playgroud)

Jos*_*ron 2

我找到了解决上述问题的方法。如果您提取 try catch 块并将其放入不同的方法中,则域类保存确实会被持久化。

IE

callMethodThatContainsTryCatch()
domainClass.save()
Run Code Online (Sandbox Code Playgroud)

代替

try {
} catch (Exception exception) {
    println "some exception"
}

domainClass.save()
Run Code Online (Sandbox Code Playgroud)

然后,您的方法中的任何异常都会回滚该方法中的所有 gorm 事务。