I am trying to create a POC for using GORM outside Grails. I created a groovy-gradle standalone plugin. I followed the official docs and
made a count() query to my User domain in main() class. While running the application using gradle run command came across this error:
Exception in thread "main" org.springframework.dao.DataAccessResourceFailureException: Could not obtain current Hibernate Session; nested exception is org.hibernate.HibernateException: No Session found for current thread
at org.grails.orm.hibernate.GrailsHibernateTemplate.getSession(GrailsHibernateTemplate.java:317)
at org.grails.orm.hibernate.GrailsHibernateTemplate.doExecute(GrailsHibernateTemplate.java:273)
at org.grails.orm.hibernate.GrailsHibernateTemplate.execute(GrailsHibernateTemplate.java:230)
at org.grails.orm.hibernate.GrailsHibernateTemplate.execute(GrailsHibernateTemplate.java:116)
at org.grails.orm.hibernate.AbstractHibernateGormStaticApi.count(AbstractHibernateGormStaticApi.groovy:151)
at org.grails.datastore.gorm.GormEntity$Trait$Helper.count(GormEntity.groovy:620)
at org.grails.datastore.gorm.GormEntity$Trait$Helper$count.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
at com.foo.user.User.count(User.groovy)
at com.foo.user.User$count.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:117)
at com.foo.utils.EntityInitializerUtils.initializeDomains(EntityInitializerUtils.groovy:45)
at com.foo.utils.EntityInitializerUtils.initializeDomains(EntityInitializerUtils.groovy:21)
at com.foo.utils.EntityInitializerUtils$initializeDomains.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
at com.foo.Application.main(Application.groovy:15)
Caused by: 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:688)
at org.grails.orm.hibernate.GrailsHibernateTemplate.getSession(GrailsHibernateTemplate.java:315)
I have created a Github repo which reproduces this issue. Tried a few solutions like wrapping the dynamic query call in a new transaction manager, Modified the DB configuration etc to resolve this error but nothing worked. I must be missing something silly here, but I have not been able to figure it out yet.
You are getting the error because nowhere in your code are you starting a Hibernate session. There are several ways you can address this. One is you can start a session explicitly...
User.withNewSession {
println User.count()
}
You can see that at https://github.com/jeffbrown/hibernate-gorm-poc/commit/3b7f747286b3c12367e9ab57007c57a06b493bcc.
Another more robust option that has a bunch of unrelated benefits is to use GORM Data Services. See https://github.com/jeffbrown/hibernate-gorm-poc/commit/0ba9b609ebbed9d5784d17e759275958b344004f.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With