Search code examples
grailsgrails-ormmulti-tenantspring-transactionsgrails3

Difference between Grails withTransaction() and a transactional service method


I'm working on a Grails 3 application with a multi-tenant DB. Understandably, for connection pool performance reasons, any query to the multi-tenant DB needs to be in a transaction. I don't have the link but Graeme Rocher outlines it somewhere on SO.

So it works fine when I do a:

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

but when I move that to a service method

@Transactional
class MyService {
    doStuffHere() {
    }
}

that method throws a "No session found" error as it would if I wasn't using the withTransaction() closure above.

Anybody know why the difference? Is there something else I should set on the service? It seems redundant to use a withTransaction() inside the service's doStuffHere() method above.


Solution

  • Have a look at the third paragraph of Burt's answer : What is the difference between withTransaction and withSession in grails?

    'withTransaction' will create a session if required. '@Transactional' will not.