运行 grails 中的一个线程 groovy

running a thread in grails groovy

我正在使用 grails "Promise" API 到 运行 一个线程。

线程正在执行以下操作

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

我只是不知道如何解决我面临的问题。

无论如何,我遇到了以下异常

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.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.call(PGroup.java:289)
at groovyx.gpars.group.PGroup.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)

在此处查看 "Multiple Asynchronous GORM calls" 部分:http://docs.grails.org/latest/guide/async.html#asyncGorm。它说 Domain.async.task{} 为您处理将新会话绑定到异步线程,而 Promises.task 则不会。

如果您无法使用Domain.async.task{},您也可以尝试Domain.withNewSession{}方法。