HibernateException:当GORM查询移动到另一个域类时,找不到当前线程的会话 [英] HibernateException: No Session found for current thread when GORM query moved into another domain class

查看:104
本文介绍了HibernateException:当GORM查询移动到另一个域类时,找不到当前线程的会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Grails中,我有一个Domain类,可以在BootStap.groovy中查询



pre $ def cref = AppXref.find { user_nm =='john'}

然而,一旦我将代码移入另一个Domain类的方法我会有以下错误。

  Servlet.service()用于上下文中带有path [/ myapp]的servlet [default]引发异常
消息:无法获取当前的Hibernate Session;嵌套异常是org.hibernate.HibernateException:找不到当前线程的会话

这里是我的hibernate配置Config.groovy

$ h $ {
cache.use_second_level_cache = true
cache.use_query_cache = false
// cache.region.factory_class ='net.sf.ehcache.hibernate.EhCacheRegionFactory'// Hibernate 3
cache.region.factory_class ='org.hibernate.cache.ehcache.EhCacheRegionFactory'// Hibernate 4
singleSession = true //配置OSIV singleSession模式
flush.mode ='manual'//在事务上下文之外的OSIV会话刷新模式
}

我将cache.use_query_cache更改为true。但它没有区别。

解决方案

域类方法不是事务性的,所以您必须确保它们被调用TX-context:将它们放入服务中,或使用 .withTransaction {}


In grails, I have a Domain class and can be queried in BootStap.groovy

def xref = AppXref.find{user_nm == 'john'}

However, once I moved the code into a method of another Domain class I will have the following error.

Servlet.service() for servlet [default] in context with path [/myapp] threw exception
Message: Could not obtain current Hibernate Session; nested exception is org.hibernate.HibernateException: No Session found for current thread

Here is my hibernate config in Config.groovy

hibernate {
    cache.use_second_level_cache = true
    cache.use_query_cache = false
//    cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory' // Hibernate 3
    cache.region.factory_class = 'org.hibernate.cache.ehcache.EhCacheRegionFactory' // Hibernate 4
    singleSession = true // configure OSIV singleSession mode
    flush.mode = 'manual' // OSIV session flush mode outside of transactional context
}

I changed cache.use_query_cache to true. But it made no difference.

解决方案

domain class methods are NOT transactional, so you have to make sure, that they are invoked in a TX-context: either put them in a service, or use .withTransaction{}

这篇关于HibernateException:当GORM查询移动到另一个域类时,找不到当前线程的会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆