Reputation: 1449
I am new to hibernate and I have a little confusion in retrieving sessions using getCurrentSession() and openSession(), many articles mentioned that getCurrentSession creates a brand new session if one is not already existing and does not create a new session if a session is already available, and on the other hand openSession creates a brand new session.
Can you please help me to clarify this and apologies for any mistake I made
thanks a lot
Upvotes: 0
Views: 518
Reputation: 4784
openSession()
binds the session to the current context. Default context is Thread
, it can also be bound on HttpRequest
level using custom filter or OpenSessionInViewFilter
getCurrentSession()
is called for the first time in that particular context. after that to find already existing session Hibernate uses CurrentSessionContext
to determine current context and will return session bound to that particular context. Majority of Application Servers associates a session with a Thread
which is created using the underlying ThreadLocal
object.Upvotes: 1