-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Transaction.rollback() and Session cache
PostPosted: Thu May 12, 2005 1:12 pm 
Expert
Expert

Joined: Wed Apr 06, 2005 5:03 pm
Posts: 273
Location: Salt Lake City, Utah, USA
Hibernate version:2.1.3

I'm trying to understand how the session cache works with rollback. I have some code that:

1 - opens a session
2 - creates a transient object
3 - begins a transaction - Session.beginTransaction()
4 - saves the transient object - Session.saveOrUpdateCopy() (which should be the same as a simple Session.save() in this case, right?)
5 - rolls back the transaction - Transaction.rollback()
6 - later tries to lookup the object in the same session - Session.get()

Since the transaction was rolled back, and the object was never persisted, I would expect that the call to Session.get() would return null. Instead it returns the transient object. I'm guessing that I just don't understand the semantics of some of these methods, but it seems like misleading behavior for Session.get() to return the object in this case. I'm guessing it's happening because the transient object was placed in the session cache, and not removed when the transaction was rolled back.

Am I expected to close the session after a call to Transaction.rollback()? I couldn't find anywhere in the documentation that mentions this.

Thanks in advance for your help.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 13, 2005 2:01 am 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
When transaction fails it is best for you to clear or close session, since most of the times exceptions are unrecoverable.
Session is almost unaware of transaction - since you can do all the same things even if you don't use Session.beginTransaction. It is up to you if you use autoCommit (not a good usage) or commit/rollback the transaction your self.
So it is a normal behaviour that you get object from session even if you rolled back you transaction - 'parallel' independance of session and transaction.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 13, 2005 10:36 am 
Expert
Expert

Joined: Wed Apr 06, 2005 5:03 pm
Posts: 273
Location: Salt Lake City, Utah, USA
alesj wrote:
When transaction fails it is best for you to clear or close session, since most of the times exceptions are unrecoverable.
Session is almost unaware of transaction - since you can do all the same things even if you don't use Session.beginTransaction. It is up to you if you use autoCommit (not a good usage) or commit/rollback the transaction your self.
So it is a normal behaviour that you get object from session even if you rolled back you transaction - 'parallel' independance of session and transaction.


Thanks for the reply. I guess I just didn't consider rolling back a transaction to be equivalent to an unrecoverable exception. It seems like if you had a session and wanted to execute a sequence of transactions, if one in the middle fails and is rolled back (e.g. the user cancels an operation), you should be able to continue with the rest of the transactions in the same session (i.e. continuing to use the session cache since it contains objects from the previous successful transactions).

So if my session cache is messed up after rolling back a transaction, is clearing the session sufficient, or do I need to close the session? I am wrapping the Hibernate Session in my own session object anyway, so it's easy for me to call Session.clear() in my rollback method, but I want to make sure that's ok. Does anyone know?

Thanks in advance.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.