0

In past projects which were using previous versions of Grails/Hibernate I have used in services funciton:

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

and for example during long tasks like bulk updates I have used it e.g.:

    bigListToIterate.each {

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

However this is not working for Grails 4.0.1; athough no exception is thrown, the changes are still not reflected in the db.

How can I get it done?

I'm using:

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

Mysql version:

SELECT version();
'5.7.21'

If I inspect database, I can see that number of Rows in the table is increasing and Data Length of the table is increasing, however when I run MySQL query select * from table I can not get any results until the whole function has ended.

enter image description here

Michal_Szulc
  • 4,097
  • 6
  • 32
  • 59

1 Answers1

1

If you need to do some sort of batch-processing with your domain object, the standard way would be to call .save( flush:true ) each n times:

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

That would flush session for you.

injecteer
  • 20,038
  • 4
  • 45
  • 89