Originally posted by Bear Bibeault:
I guess I'm still confused as to what's happening in my code snippet. If a session is opened with openSession() I'd assume that a new session is created. I'd then expect a call to getCurrentSession() to return that session rather than create a new one. If a new one is created by getCurrentSession() but subsequent calls to getCurrentSession() return that same session, under what conditions does getCurrentSession() create a new session and when does it return the existing session?
What I think is being said is that openSession() and getCurrentSession() don't talk to each other, or should I say, openSession() just returns you a Session and doesn't do anything internally to store it in some variable, so that it can know to either create a new Session or return the one already created.
Also meaning that is you are going to use getCurrentSession() Hibernate expects that that is all you are going to be calling, that you would never be calling openSession() at all, even at first.
Or easier put, lets look at the Hibernate code for openSession() at least the version of the method that all the other versions call.
It basically creates new and returns it on the same line.
Now if we look at the getCurrentSession() method it is actually looking to use a currentSessionContext, which stores the Session, or creates new, if the context hadn't created a Session yet.
This is definitely just a design decision, that does a great job of seperation of concerns here. Basically saying why add the whole extra context in openSession() if it isn't necessary, since most people will be using openSession in their context of not using getCurrentSession. Also to keep openSession() backwards compatible with older code written to older Hibernate code.
And I am also just conjecturing here, not having been in the room when the Hibernate guys made their design decisions.
Mark