How to force flushing in Grails GORM

孤者浪人 提交于 2019-12-24 03:51:50

问题


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)


回答1:


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()
}



回答2:


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

obj.save flush:true



回答3:


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)



回答4:


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()


来源:https://stackoverflow.com/questions/30486949/how-to-force-flushing-in-grails-gorm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!