Search code examples
multithreadinggrailsgroovysession-state

running a thread in grails groovy


I am using grails "Promise" API to run a thread.

Thread is performing the following actions

 csv.splitEachLine(',') { row ->
                insertRecord = Code.findByAni(row[0]) ?: new SurveyProActive(
                        cid: row[1],
                        ani: row[0],
                        data_time: new Date(),
                        dnis: Code2.findById((Long) row[2])?.dialNumber,

                ).save(failOnError: true, flush: true)
            }
        }

I am just no getting how to solve the problem I am facing .

Anyhow, I am getting the following exceptions

ERROR org.grails.web.errors.GrailsExceptionResolver - HibernateException occurred when processing request: [POST] /Survey/proactiveSurvey - parameters:
a: 17
No Session found for current thread. Stacktrace follows:
org.hibernate.HibernateException: No Session found for current thread
at org.grails.orm.hibernate.GrailsSessionContext.currentSession(GrailsSessionContext.java:116)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:687)
at org.grails.orm.hibernate.HibernateSession.createQuery(HibernateSession.java:176)
at org.grails.orm.hibernate.HibernateSession.createQuery(HibernateSession.java:170)
at org.grails.datastore.gorm.finders.AbstractFindByFinder.buildQuery(AbstractFindByFinder.java:44)
at org.grails.datastore.gorm.finders.AbstractFindByFinder$1.doInSession(AbstractFindByFinder.java:29)
at org.grails.datastore.mapping.core.DatastoreUtils.execute(DatastoreUtils.java:318)
at org.grails.datastore.gorm.finders.AbstractFinder.execute(AbstractFinder.java:42)
at org.grails.datastore.gorm.finders.AbstractFindByFinder.doInvokeInternal(AbstractFindByFinder.java:27)
at org.grails.datastore.gorm.finders.DynamicFinder.invoke(DynamicFinder.java:174)
at org.grails.datastore.gorm.finders.DynamicFinder.invoke(DynamicFinder.java:374)
at org.grails.datastore.gorm.GormStaticApi.methodMissing(GormStaticApi.groovy:173)
at org.grails.datastore.gorm.GormEntity$Trait$Helper.staticMethodMissing(GormEntity.groovy:749)
at com.ef.apps.pcs.SurveyController$_proactiveSurvey_closure4.doCall(SurveyController.groovy:334)
at org.grails.plugins.web.async.WebRequestPromiseDecorator.invokeClosure(WebRequestPromiseDecorator.groovy:43)
at org.grails.plugins.web.async.WebRequestPromiseDecorator$_decorate_closure1.doCall(WebRequestPromiseDecorator.groovy:30)
at groovyx.gpars.group.PGroup$3.call(PGroup.java:289)
at groovyx.gpars.group.PGroup$4.run(PGroup.java:313)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)

Solution

  • Check out the "Multiple Asynchronous GORM calls" section here: http://docs.grails.org/latest/guide/async.html#asyncGorm. It says that Domain.async.task{} deals with binding a new session to the asynchronous thread for you, whereas Promises.task does not.

    If you are not able to use Domain.async.task{} you could also try the Domain.withNewSession{} method.