![](/img/trans.png)
[英]What is the difference between withTransaction and withSession in grails?
[英]Difference between Grails withTransaction() and a transactional service method
我正在使用带有多租户DB的Grails 3应用程序。 可以理解的是,出于连接池性能的原因,对多租户DB的任何查询都必须在事务中进行。 我没有链接,但是Graeme Rocher在SO的某个位置概述了该链接。
因此,当我执行以下操作时,它可以正常工作:
MyDomainClass.withTransaction { status ->
doStuffHere();
}
但是当我将其移至服务方法时
@Transactional
class MyService {
doStuffHere() {
}
}
如果我不使用上面的withTransaction()闭包,该方法将引发“找不到会话”错误。
有人知道为什么会有区别吗? 我应该在服务上设置其他内容吗? 在上面的服务的doStuffHere()方法中使用withTransaction()似乎是多余的。
看一下Burt答案的第三段: grails中的withTransaction和withSession有什么区别?
如果需要,“ withTransaction”将创建一个会话。 '@Transactional'不会。
主要区别在于它们如何指示交易范围。
withTransaction使用事务覆盖了块中的代码。
@Transactional执行相同的操作,但是方法中包含代码。
还要注意,withTransaction和@Transactional( 不带任何参数 )都使用PROPAGATION_REQUIRED,因此在事务性代码块中调用时,它将使用现有事务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.