栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在Grails服务中回滚事务

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在Grails服务中回滚事务

是的,那样做。

默认情况下,Grails中的事务是在Service方法级别处理的。如果该方法正常返回,则将提交事务,如果引发RuntimeException,则将回滚该事务。

请注意,这意味着即使在将对象保存在服务器方法中的同时使用flush:true,如果抛出RuntimeException,数据库更改仍将回滚。

例如:

class MyService { def fiddle(id,id2){   def domain = Domain.findById(id)   domain.stuff = "A change"   domain.save( flush:true ) // will cause hibernate to perform the update statements   def otherDomain = OtherDomain.findById(id2)   otherDomain.name = "Fiddled"   if( !otherDomain.save( flush:true )){ // will also write to the db     // the transaction will be roled back      throw new RuntimeException("Panic what the hell happened")   }     }}

我对Grails的理解不是100%清楚的是,如果在直接的java /
spring世界中抛出了检查异常,将会发生什么,默认行为是让事务接收器提交事务,因此可以在配置中覆盖它。

注意:有一个警告,那就是您的数据库必须支持您要更新的表上的事务。是的,这是MySQL的问题:)

这也适用于该

Domain.withTransaction
方法。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/446365.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号