Reputation: 154
I have a service that distributes tasks to operators. Inside a method I distribute many tasks in time inside a loop. I want to flush the task, the operator, and a DistributionLog. If I just had one domain to save I think I could do something like
Operator.withTransaction{ //...some code }
but I have at least 3 domains to save and to make it even worse, two of them have dependency on each other. The operator have a list of tasks.
I can't wait all the distribution to finish before an operator can get his tasks, so I have to force it to flush. To make it even harder, it's all inside a multitenantService.doWithTenant() (multitenant plugin)
Upvotes: 4
Views: 7968
Reputation: 350
You can get the session using the withSession
method available in all domain classes and call to flush()
on it.
Operator.withSession { session ->
// ...
session.flush()
}
Upvotes: 6
Reputation: 3694
If you want to do an explicit flush, you can get a reference to the hibernate session factory in your grails service like this:
def sessionFactory
You can then get the current hibernate session, and call flush on that:
sessionFactory.currentSession.flush()
Upvotes: 4
Reputation: 466
See the documentation:
http://grails.github.io/grails-doc/2.2.5/ref/Domain%20Classes/save.html
The save method informs the persistence context that an instance should be saved or updated. The object will not be persisted immediately unless the flush argument is used:
b.save(flush: true)
Upvotes: 0
Reputation: 7985
You can force a flush with flush
argument to the last call to save
:
obj.save flush:true
Upvotes: 2