Michael
Michael

Reputation: 33297

Transaction Scope in Grails?

I use Grails Hibernate transactions to do the following in a Controller Action:

user.save(flush:true)

User.withTransaction {
  user.name = "newName"
  user.save(flush: true)
}

What does Hibernate/Grails do in this case? When I flush the hibernate session on the first flush does the withTransaction creates a new session or will it attach the two saves in one session?

Upvotes: 1

Views: 537

Answers (1)

Burt Beckwith
Burt Beckwith

Reputation: 75671

If it's in a controller action, then there will be a Hibernate session open the whole time since the OpenSessionInView interceptor starts that for you at the beginning of the request. The withTransaction call uses the current thread-local session.

Note that transaction will flush the session, so there's no need to do it explicitly. Also note that you really shouldn't be polluting controller code with transactions and other persistence (or business) logic - this code should be in a transactional service method.

Upvotes: 4

Related Questions