Reputation: 205
I am confused between withSession in Grails and current hibernate session.
My question is: Is the session object which we have access to in the closure the same as the current hibernate session object?
I wrote a Service that has an action as 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 for me is that there are 5 different hash codes... If I print the 5 session objects, I see the same toString() result. That makes me guess that they have the same contents:
SessionImpl(PersistenceContext[entityKeys=[EntityKey[com.astek.agileFactory.Link#170], EntityKey[com.astek.agileFactory.Project#9]],collectionKeys=[Coll......"
Upvotes: 4
Views: 2515
Reputation: 50245
To answer your question briefly:
The session object which we have access to in closure is not the current hibernate session.
The session object is a proxy of the current hibernate session. Hence different hash codes in each case.
Have a look at source of withSession, clearly seen setExposeNativeSession
is set to false
(default value is false as well) in HibernateTemplate which makes sure to always return a Session Proxy without exposing the native hibernate session.
Upvotes: 5