Grails withSession and the current hibernation session

I am confused between withSession in Grails and the current hibernate session.

My question is: is there a session object that we have access to in closure that is the same as the current sleep mode session object?

I wrote a Service that has an action as shown below:

def strangeBehavior(){ Link.withSession { session-> println "link current session " + session.hashCode() } Task.withSession { session-> println "task current session " + session.hashCode() } Project.withSession { session-> println "project current session " + session.hashCode() } UserStory.withSession { session-> println "user story current session " + session.hashCode() } def ctx = AH.application.mainContext def sessionFactory = ctx.sessionFactory def tmp = sessionFactory.currentSession println " current session " + tmp.hashCode() } } 

What is strange to me is that there are 5 different hash codes ... If I print 5 session objects, I see the same result toString (). This makes me guess that they have the same content:

SessionImpl (PersistenceContext [entityKeys = [EntityKey [com.astek.agileFactory.Link # 170], EntityKey [com.astek.agileFactory.Project # 9]], collectionKeys = [Coll ...... "

+4
source share
1 answer

To briefly answer your question:
The session object that we have access to close is not the current hibernation session.

The session object is the proxy server for the current sleep mode session. Therefore, different hash codes in each case.

Look at the source withSession , it is clear that setExposeNativeSession set to false (the default value is also false) in the HibernateTemplate , which should always return the session proxy without exposing the session to its native sleep mode.

+4
source

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


All Articles