Grails:在Promise.onComplete()

时间:2017-05-23 17:48:21

标签: hibernate session grails promise

Grails 3.2.9

执行任务后,onComplete方法无法获得休眠会话。

控制器:

def addBranch() {
    Branch newBranch = new Branch(name: branchName, root: newBranchRoot, 'active':false).save();    
    Promise p = gitService.asyncPull(newBranch)     
    p.onComplete { b ->
        println "finished adding branch " + branchName;         
        newBranch.setActive(true);
        newBranch.save();  <<<=== EXCEEPTION THROWN HERE
    }       
    p.onError { Throwable err ->
        println "An error occurred ${err.message}"          
    }       
    respond(status:"ok", message:"success!");       
}   

服务:

public Promise<Branch> asyncPull(Branch branch) {
    WebPromises.task {          
        initBranch(branch);
        pull (branch.name);
        branch;
    }
}

异常堆栈跟踪

2017-05-23 13:41:29.123 [ead 6]错误异步执行错误:无法获取当前的Hibernate会话;嵌套异常是org.hibernate.HibernateException:找不到c的会话 你的主题

org.springframework.dao.DataAccessResourceFailureException:无法获取当前的Hibernate会话;嵌套异常是org.hibernate.HibernateException:找不到c的会话 你的主题         在org.grails.orm.hibernate.GrailsHibernateTemplate.getSession(GrailsHibernateTemplate.java:317)         在org.grails.orm.hibernate.GrailsHibernateTemplate.doExecute(GrailsHibernateTemplate.java:273)         在org.grails.orm.hibernate.GrailsHibernateTemplate.execute(GrailsHibernateTemplate.java:230)         在org.grails.orm.hibernate.GrailsHibernateTemplate.execute(GrailsHibernateTemplate.java:116)         at org.grails.orm.hibernate.validation.UniqueConstraint.processValidate(UniqueConstraint.java:149)         at grails.validation.AbstractConstraint.validate(AbstractConstraint.java:107)         at grails.validation.ConstrainedProperty.validate(ConstrainedProperty.java:979)         at org.grails.validation.GrailsDomainClassValidator.validatePropertyWithConstraint(GrailsDomainClassValidator.java:206)         在org.grails.validation.GrailsDomainClassValidator.validate(GrailsDomainClassValidator.java:81)         在org.grails.orm.hibernate.AbstractHibernateGormInstanceApi.save(AbstractHibernateGormInstanceApi.groovy:122)         在org.grails.datastore.gorm.GormEntity $ Trait $ Helper.save(GormEntity.groovy:151)         在org.grails.datastore.gorm.GormEntity $ Trait $ Helper $ save $ 11.call(Unknown Source)         在org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)         在org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)         在org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:133)         在com.care.incasa.Branch.save(Branch.groovy)         在com.care.incasa.Branch.save(Branch.groovy)         在org.grails.datastore.gorm.GormEntity $ save $ 1.call(未知来源)         在org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)         在org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)         在org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)         在com.care.incasa.AdminController $ _addBranch_closure1 $$ EQKTGeEk.doCall(AdminController.groovy:43)         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)         at java.lang.reflect.Method.invoke(Method.java:498)         at org.springsource.loaded.ri.ReloadedTypeInvoker $ 2.invoke(ReloadedTypeInvoker.java:133)         at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1461)         在org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)         at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)         在org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:294)         at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1027)         在org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:42)         在org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)         在org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:57)         在org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)         在org.grails.plugins.web.async.AsyncWebRequestPromiseDecorator.invokeClosure(AsyncWebRequestPromiseDecorator.groovy:91)         at org.grails.plugins.web.async.AsyncWebRequestPromiseDecorator $ _decorate_closure2.doCall(AsyncWebRequestPromiseDecorator.groovy:67)         at org.grails.plugins.web.async.AsyncWebRequestPromiseDecorator $ _decorate_closure2.call(AsyncWebRequestPromiseDecorator.groovy)         在org.grails.async.factory.gpars.GparsPromise $ _onComplete_closure1.doCall(GparsPromise.groovy:92)         在org.grails.async.factory.gpars.GparsPromise $ _onComplete_closure1.call(GparsPromise.groovy)         at groovyx.gpars.dataflow.DataCallback $ 1.run(DataCallback.java:62)         在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)         at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:617)         在java.lang.Thread.run(Thread.java:745) 引起:org.hibernate.HibernateException:找不到当前线程的Session         在org.grails.orm.hibernate.GrailsS​​essionContext.currentSession(GrailsS​​essionContext.java:116)         在org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:688)         在org.grails.orm.hibernate.GrailsHibernateTemplate.getSession(GrailsHibernateTemplate.java:315)         ...省略了44个常见帧

2 个答案:

答案 0 :(得分:0)

对于这种情况,您应该使用withTransactionwithSession来自行处理交易上下文:

p.onComplete { b ->
  Branch.withTransaction{
    println "finished adding branch $branchName"        
    newBranch.active = true
    newBranch.save()
  }
}    

答案 1 :(得分:0)

谢谢,提到的更改有效:

Promise<Branch> p = gitService.asyncPull(newBranch)     
p.onComplete { b ->
  Branch.withTransaction { session ->               
    Branch br = Branch.findByName(b.name);
    br.setActive(true);
    br.save(flush:true);
  }
}