HibernateException: session not found for current thread when GORM request moved to another domain class

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

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

However, as soon as I moved the code to 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 hibernation configuration 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 that didn't matter.

+4
source share
2 answers

domain class methods are NOT transactional, so you must ensure that they are called in the TX context: either put them in the service or use .withTransaction{}

+6
source

Adding a @Transactionalmethod that worked for me.

+5

Source: https://habr.com/ru/post/1569564/


All Articles