-->
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.  [ 6 posts ] 
Author Message
 Post subject: transaction rollback
PostPosted: Wed May 17, 2006 12:40 pm 
Newbie

Joined: Tue Nov 01, 2005 1:39 pm
Posts: 7
Hi. I have a simple question regarding transaction rollback.
When I execute the following code:

Code:
Transaction transaction1 = dbSession.beginTransaction();
// create persistant class 1
transaction1.rollback();

Transaction transaction2 = dbSession.beginTransaction();
// create persistant class 2
transaction2.commit();


both persistant classes 1 and 2 are stored in the database. I'm not sure I understand why.
One resolution I found for this is adding transaction1.flush() before transaction1.rollback(). However, I didn't notice any documentation/example in which rollback requried a flush or clear of the session.
Am I doing something wrong or missing something here?

I appreciate the help



Hibernate version: 3.1 final

Mapping documents: using annotations

Name and version of the database you are using: oracle 10g

The generated SQL (show_sql=true):
Hibernate: select ATTR_SEQ.nextval from dual
Hibernate: select ATTR_SEQ.nextval from dual
Hibernate: insert into STATIC_ATTR (CREATION_TIMESTAMP, PARENT_ID, PARENT_TYPE, PRIORITY_WEIGHT, NAME, TYPE, ID) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into STATIC_ATTR (CREATION_TIMESTAMP, PARENT_ID, PARENT_TYPE, PRIORITY_WEIGHT, NAME, TYPE, ID) values (?, ?, ?, ?, ?, ?, ?)


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 17, 2006 12:45 pm 
Regular
Regular

Joined: Wed Aug 25, 2004 6:23 am
Posts: 91
The transaction does not affect local object states or unregister objects that you've registered with the session - it's purely a database level thing. As the newly created object is still part of the session it is written to the database when you perform the commit operation.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 17, 2006 1:07 pm 
Newbie

Joined: Tue Nov 01, 2005 1:39 pm
Posts: 7
thanks for the quick reply.
Just to state my opinion about this, I think it is completely against the "units of work" idea. If I perform some modifications (in this case, save an object to database) in context of a transaction, and then rollback the transaction, I would expect that modification to be undone.
Do you think that performing a flush before a rollback is the correct thing to do (even specifically in this case)?
Moreover, It is stated that transaction.commit also flush the session. maybe transaction.rollback should do the same?


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 17, 2006 1:11 pm 
Regular
Regular

Joined: Wed Aug 25, 2004 6:23 am
Posts: 91
The unit of work idea is typically applied to sessions rather than transactions. See http://www.hibernate.org/42.html for a good explanation.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 18, 2006 5:34 pm 
Newbie

Joined: Tue Jan 04, 2005 7:54 am
Posts: 3
r1ch wrote:
The unit of work idea is typically applied to sessions rather than transactions. See http://www.hibernate.org/42.html for a good explanation.


Yes - but transactions and sessions do depend on each other.

IMHO there are SURPRISES in current design.

What we have:

IF session.flushMode != NEVER
THEN tr.commit() calls session.flush()


(which is quite OK for me).
but I have not seen something like this in tr.rollback()

and it is VERY EASY to make a mistake ( just by calling flush on session
after rollback) to insert inconssistent data into db.
(if only JDBC connections with default autocommit are used)

If I were implementing it I would made tr.rollback() calling session.flush() or even session.clear()


Top
 Profile  
 
 Post subject: Re: transaction rollback
PostPosted: Fri May 19, 2006 8:11 pm 
Newbie

Joined: Tue May 02, 2006 6:54 pm
Posts: 10
Location: Salt Lake City, Utah
You can use Session.FlushMode = FlushMode.Commit instead of flushing before rollback.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.