在Grails 4.0.1中刷新会话

问题描述 投票:0回答:1

在过去使用Grails / Hibernate以前版本的项目中,我曾在服务功能中使用过:

private void cleanGorm() {
    def session = sessionFactory.currentSession
    session.flush()
    session.clear()
}

例如,在诸如批量更新之类的长期任务中,我使用了它,例如:

    bigListToIterate.each {

        if (it.id > 0 && it.id % 50 == 0) {
            cleanGorm()
        }
        //CRUD action
    }

但是这不适用于Grails 4.0.1;尽管没有引发异常,但更改仍未反映在数据库中。

如何完成?

我正在使用:

compile "org.grails.plugins:hibernate5"
compile "org.hibernate:hibernate-core:5.4.0.Final"

如果检查数据库,我可以看到表中的行数正在增加,并且表的数据长度正在增加,但是当我运行MySQL查询select * from table时,直到整个函数结束,我才能获得任何结果。

hibernate grails flush hibernate-5.x grails-4
1个回答
0
投票
如果您需要对域对象进行某种批处理,则标准方法是每.save( flush:true )次调用n

bigListToIterate.each { Book b = new Book(...) //CRUD action b.save flush:it.id > 0 && it.id % 50 == 0 }

这将为您刷新会话。
© www.soinside.com 2019 - 2024. All rights reserved.