Search code examples
grailsgroovygrails-orm

Does findBy* require withTransaction?


I have the following services in my Grails application:

class Person {
    String name
}
class Host {
    String url
}

Then I have a method invoked by multiple concurrent threads:

def person = Person.findByName("Coco")
def host = Host.findByUrl("some_url")

Do I need to surround both queries with a *.withTransaction { } block? E.g.:

Person.withTransaction { def person = Person.findByName("Coco") }
Host.withTransaction { def host = Host.findByUrl("some url") }

I have read the findBy* documentation but couldn't find anything about DB transactional safety.

Thank you.


Solution

  • Do I need to surround both queries with a *.withTransaction { } block?

    Whether or not you want that to be happening inside of a withTransaction block depends on potentially a number of factors in your app but to answer the question, no. Those queries can be executed anywhere in your app.