Grails @Transactional与Spring @Transactional注解之间的差异 [英] Differences between the Grails @Transactional vs. the Spring @Transactional annotations

查看:173
本文介绍了Grails @Transactional与Spring @Transactional注解之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Grails 服务文档有一点。它说:

Well there was a point in the Grails service documentation. It said:


grails.transaction.Transactional注释在Grails 2.3中首次引入。在2.3之前,使用了Spring的@Transactional注解。

The grails.transaction.Transactional annotation was first introduced in Grails 2.3. Prior to 2.3, Spring's @Transactional annotation was used.

但是我似乎无法找出这两者之间的主要区别注释是。为什么Spring的注释未在以后的版本中使用?


But I can't seem to find out what the main difference between those two annotations is. Why was the Spring's annotation not used in future releases?

推荐答案

我想但是,在2.3版本中,团队认为这是一个好主意(我个人不同意)引入一个新的注释,它不仅可以应用于服务,还可以应用于控制器。

I would like to address this comment "However, in 2.3 the team felt it was a good idea (I disagree personally) to introduce a new annotation which could be applied not only to Services but also to Controllers."

Grails团队的主要目的不是引入可用于控制器和服务的注释,我们的主要目的是引入AST转换,消除了对代理的需求,并且表现比Spring更好@Transactional。Grails的@Transactional将处理事务的必要逻辑直接连接到字节码中,并且不需要代理,因此我们认为它比Spring的版本更好。

It was never the primary intention of the Grails team to introduce an annotation that could be used on both controllers and services. Our primary intention was to introduce an AST transform that eliminated the need for a proxy and performed better than Spring’s @Transactional. Grails’ @Transactional wires the necessary logic for dealing with transactions directly into the byte code and doesn’t need a proxy hence why we feel it is better than Spring’s version.

它也适用于控制器的事实仅仅是我们发现的上述副作用。在我们的经验中说过很多Grails用户在使用控制器时没有正确划分他们的事务边界。如果您将控制器操作标记为只读,那么Hibernate无需对在操作范围内执行的任何查询执行脏检查。这极大地提高了Grails应用程序的性能,并且非常好。

The fact that it also works on controllers is merely a side effect of the above that we discovered. Having said that in our experience too many Grails users do not correctly demarcate their transactional boundaries when using controllers. If you mark a controller action as being read-only then Hibernate does not need to perform dirty checking for any queries you do within the scope the action. This greatly improves performance of Grails applications and can very much be a good thing.

我们绝对鼓励将逻辑分解为服务,但是如果您考虑以下简单示例:

We absolutely encourage separation of logic into services, but if you consider the following trivial example:

  @Transactional(readOnly=true)
  def show(Long id) {
       respond Foo.get(id)
  }

引入一个用于这样一个简单操作的服务是矫枉过正的,但是如果你不用一个只读事务划分查询,那么Hibernate

It is overkill to introduce a service for such a trivial action, but if you don’t demarcate the query with a read-only transaction then Hibernate performs a dirty check on the instance which hurts performance.

Update 22/02/16:从Grails 3.1开始,Spring版本被认为不推荐使用并在默认情况下被禁用(如果需要,您仍然可以重新启用)

Update 22/02/16: As of Grails 3.1 the Spring version is considered deprecated and is disabled by default (you can still re-enable if necessary)

这篇关于Grails @Transactional与Spring @Transactional注解之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆