3

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)

Burt Beckwith
  • 75,342
  • 5
  • 143
  • 156
user3155328
  • 144
  • 1
  • 7
  • I'm sorry Ithink I should have informed that I have already tried to call flush on the last .save() call, but it didn't work. I don't know if it is because all is happening inside the doWithTenant but what I know is that it didn't work – user3155328 May 27 '15 at 16:10

4 Answers4

6

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()
}
albertovilches
  • 340
  • 1
  • 5
3

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()
rcgeorge23
  • 3,594
  • 4
  • 29
  • 54
2

You can force a flush with flush argument to the last call to save:

obj.save flush:true
Graeme Rocher
  • 7,985
  • 2
  • 26
  • 37
  • 1
    The Object is not flushed right away. Only when the Transaction is commited, or the session is flushed, the Object is persisted. – IndianerJones Jan 20 '20 at 08:55
0

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)
Yaroslav
  • 446
  • 4
  • 15