【问题标题】:Flush the session in Grails 4.0.1在 Grails 4.0.1 中刷新会话
【发布时间】:2020-01-19 22:41:49
【问题描述】:

在过去的项目中,我在服务功能中使用过以前版本的 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;虽然没有抛出异常,但更改仍然没有反映在 db 中。

我怎样才能完成它?

我正在使用:

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

Mysql版本:

SELECT version();
'5.7.21'

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

【问题讨论】:

    标签: hibernate grails flush hibernate-5.x grails-4


    【解决方案1】:

    如果您需要对域对象进行某种批处理,标准方法是调用.save( flush:true ) 每次n 次:

    bigListToIterate.eachWithIndex { something, index ->
        Book b = new Book(...)
        //CRUD action
        b.save flush:index > 0 && index % 50 == 0
    }
    

    这将为您刷新会话。

    【讨论】:

    • 查看更新。当然,您必须刷新index 而不是id
    • 我已经尝试过了,但仍然没有运气。不幸的是,它的行为仍然相同:在整个循环完成之前,没有数据会保存在 db 中。
    • 在会话刷新后立即添加 prinltn Book.count() 之类的内容。为了好玩你也可以做一些sleep 100
    • prinltn Book.count() 显示正确的数字。但是它没有反映在 db 中
    • 这意味着你的 db-ciient 是滞后的
    猜你喜欢
    • 2014-02-18
    • 1970-01-01
    • 2012-06-10
    • 2019-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-08
    相关资源
    最近更新 更多