HibernateException: No Session found for current thread when GORM query moved into another domain class

匿名 (未验证) 提交于 2019-12-03 01:58:03

问题:

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.

回答1:

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



回答2:

Adding @Transactional on the method worked for me.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!