简体   繁体   中英

Grails: Difference between Author.withTransaction{} and Book.withTransaction{} if both Author and Book should be saved in that transaction

Grails version 3.2.9

Grails documentation for withTransaction doesn't say much what is the difference between using, say, Author.withTransaction and Book.withTransaction in case when I want to save both Author and Book domain instances in that transaction like the following:

Author author = Author.get(1)
Book book = Book.findByAuthor(author)

Author.withTransaction { // what if we use Book here instead of Author
    author.age = 39
    book.price = 45

    author.save(failOnError: true)
    book.save(failOnError: true)
}

Didn't find any other documentation explaining the difference.

There is no difference, they both do the same thing. Every domain class is decorated with withTransaction which is the same transnational code. You could use Foo.withTransaction and not even do anything related to Foo and it wouldn't matter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM