简体   繁体   English

withTransaction 和 withNewTransaction 有什么区别?

[英]What is the difference between withTransaction and withNewTransaction?

What is the difference between the following actions?以下操作有什么区别?

def someAction() {
  User.withTransaction { ... } 
}

and

def someAction() {
  User.withNewTransaction { ... }
}
  1. When do I use what?我什么时候用什么?

  2. When a grails action contains only a Transaction block.当 grails 操作仅包含一个事务块时。 In this case I guess withTransaction and withNewTransaction are the same, because each action has its own transaction.在这种情况下,我猜 withTransaction 和 withNewTransaction 是相同的,因为每个操作都有自己的事务。 Is this true?这是真的?

I believe these functions have to do with transaction isolation semantics.我相信这些功能与事务隔离语义有关。 The withTransaction function will participate in an existing transaction if one already has been started and would start a new one if not.如果一个事务已经开始, withTransaction函数将参与一个现有的事务,否则将开始一个新的事务。 The withNewTransaction method will always start a new transaction regardless of if one has already been started, isolating the code inside that block into it's own transaction (with its own commit/rollback). withNewTransaction方法将始终启动一个新事务,无论是否已经启动,将该块内的代码隔离到它自己的事务中(具有自己的提交/回滚)。

If you think the method you are developing should or could participate in some larger transaction with multiple separate db writes, then you should use withTransaction so that you can participate in a larger transaction if necessary.如果您认为您正在开发的方法应该或可以通过多个单独的数据库写入参与一些更大的事务,那么您应该使用withTransaction以便您可以在必要时参与更大的事务。 If you want your write here to be totally isolated from other db writes if another transaction is going on (and not potentially roll back that other transaction if this code fails), then use withNewTransaction .如果您希望在另一个事务正在进行时您的写入与其他数据库写入完全隔离(并且如果此代码失败,则不会回滚该其他事务),然后使用withNewTransaction

In regards to your question two, these two will behave the same if they are the only calls being made in an action as they would both start up a new transaction.关于您的问题二,如果这两个是在操作中进行的唯一调用,则它们的行为将相同,因为它们都将启动一个新事务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 grails中withTransaction和withSession有什么区别? - What is the difference between withTransaction and withSession in grails? Grails:Author.withTransaction {}和Book.withTransaction {}之间的区别(如果Author和Book都应保存在该事务中) - Grails: Difference between Author.withTransaction{} and Book.withTransaction{} if both Author and Book should be saved in that transaction 在 Play 中调用 JPA.withTransaction() 的开销是多少! 2.x - What is the overhead for calling JPA.withTransaction() in Play! 2.x @Entity和@embeddable有什么区别 - What is difference between @Entity and @embeddable 在Hibernate中,表达式&lt;&gt;和!=有什么区别? - What is the difference between expression <> and != in Hibernate? liquibase 和 Hibernate 有什么区别? - What is the difference between liquibase and Hibernate? LocalContainerEntityManagerFactoryBean 和 LocalEntityManagerFactoryBean 有什么区别? - What is the difference between LocalContainerEntityManagerFactoryBean and LocalEntityManagerFactoryBean? REMOVE和DELETE有什么区别? - What is the difference between REMOVE and DELETE? NHibernate 和 Hibernate 有什么区别? - What is the difference between NHibernate and Hibernate? Hibernate和JPA之间的@Entity有什么区别 - What is the difference between @Entity in Hibernate and JPA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM