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.
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.
The main difference is how they indicate the scope of the transaction.
withTransaction covers the code within the block with a transaction.
@Transactional does the same thing, but with the code inside the method.
Also note that both withTransaction and @Transactional( without any parameters ) uses PROPAGATION_REQUIRED, so it will use an existing transaction when called within a transactional block of code.
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.