【问题标题】:Difference between Grails withTransaction() and a transactional service methodGrails withTransaction() 和事务服务方法之间的区别
【发布时间】:2018-02-07 18:53:19
【问题描述】:

我正在开发一个带有多租户数据库的 Grails 3 应用程序。可以理解,出于连接池性能的原因,对多租户数据库的任何查询都需要在事务中。我没有链接,但 Graeme Rocher 在 SO 的某处概述了它。

所以当我执行以下操作时它工作正常:

MyDomainClass.withTransaction { status ->
   doStuffHere();
}

但是当我将其移至服务方法时

@Transactional
class MyService {
    doStuffHere() {
    }
}

如果我没有使用上面的 withTransaction() 闭包,该方法会引发“未找到会话”错误。

有人知道为什么会有差异吗?还有什么我应该在服务上设置的吗?在上面的服务的 doStuffHere() 方法中使用 withTransaction() 似乎是多余的。

【问题讨论】:

标签: grails grails-orm multi-tenant spring-transactions grails3


【解决方案1】:

看一下 Burt 回答的第三段:What is the difference between withTransaction and withSession in grails?

'withTransaction' 将在需要时创建一个会话。 '@Transactional' 不会。

【讨论】:

    【解决方案2】:

    主要区别在于它们如何指示事务的范围。

    withTransaction 用事务覆盖块内的代码。

    @Transactional 做同样的事情,但在方法中使用代码。

    还要注意 withTransaction 和 @Transactional(没有任何参数) 都使用 PROPAGATION_REQUIRED,因此在事务代码块中调用时它将使用现有事务。

    【讨论】:

      猜你喜欢
      • 2011-09-23
      • 2011-04-26
      • 2012-12-11
      • 2012-03-19
      • 2010-11-29
      • 2013-09-18
      • 2011-08-21
      • 2014-12-01
      • 1970-01-01
      相关资源
      最近更新 更多