views:

37

answers:

1

This question is in relation to another question I have asked, but what are the reasons as to why you would use openSession() over getCurrentSession()? I know you would use openSession() so that you could self-manage the closing and flushing of the session, however, why would you want to do this manually?

I have used openSession() when I wanted to perform a transaction in isolation to the current session though I am not sure if this is a correct use of openSession().

Why would you want multiple sessions open?

+4  A: 

I know you would use openSession() so that you could self-manage the closing and flushing of the session, however, why would you want to do this manually?

One would use openSession() to implement long conversations (i.e. when you want to use use a single Session for several database transactions aka the extended Session pattern).

I have used openSession() when I wanted to perform a transaction in isolation to the current session though I am not sure if this is a correct use of openSession().

Hmm... What? Transaction and Session are different concepts. What do you mean exactly?

Why would you want multiple sessions open?

That's not the intention.

References

Pascal Thivent
Thanks Pascal. Obviously what I'm doing to resolve my issue in my previous post is incorrect. I have a transaction that I start at point A in the code. I commit that transaction at point B. However within that transaction I need to update and commit a value in the db regardless of whether point B is reached (eg. app crashes or user terminates app manually prior to B). It looks like I can achieve this by opening a new session and starting and committing a transaction "inside" of the A -> B transaction. This didn't sound right to me to achieve an isolated transaction within the main transaction.
digiarnie