gpt4 book ai didi

Grails,抛出异常后保存域类

转载 作者:行者123 更新时间:2023-12-02 02:23:03 24 4
gpt4 key购买 nike

使用 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)

最佳答案

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

callMethodThatContainsTryCatch()
domainClass.save()

而不是

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

domainClass.save()

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

关于Grails,抛出异常后保存域类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52396027/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com