-->
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.  [ 8 posts ] 
Author Message
 Post subject: Behaviour of Transaction Rollback
PostPosted: Wed Oct 29, 2003 8:26 pm 
Newbie

Joined: Wed Aug 27, 2003 9:08 pm
Posts: 5
I noticed an interesting behaviour of Hibernate transaction when testing tx.rollback(). I loaded an existing record from database, updated one of the fields and saved it. Then I called tx.rollback(). The record was not updated in database, which was the expected behaviour of a rollback. However, if I create a new object and save it, tx.rollback() does not seem to work. The new object will always be persisted, even if tx.rollback() is called. Just wondering if this is the intended behaviour of tx.rollback(). I used a MySQL database for the testing.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 30, 2003 2:15 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Did you flush the session before rolling back? This syncs with the db and thus enables it for rollback. Alternative is the evict it from the session cache.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 30, 2003 2:55 am 
Newbie

Joined: Wed Aug 27, 2003 9:08 pm
Posts: 5
david wrote:
Did you flush the session before rolling back? This syncs with the db and thus enables it for rollback. Alternative is the evict it from the session cache.


Here is the testing code:
Code:
            SessionFactory sessionFactory = new Configuration().configure("/hibernate.cfg.xml").buildSessionFactory();
            Session session = sessionFactory.openSession();
            Transaction tx = (Transaction)session.beginTransaction();
            ExternalSystemPO newExtSystemOne = new ExternalSystemPO(EXTERNAL_SYS_KEY_ONE);
            session.save(newExtSystemOne);
            session.flush();
            tx.rollback();


session.flush() does not seem to make a difference. I will test session.evict() method to see if it works.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 30, 2003 8:52 am 
Newbie

Joined: Sun Oct 05, 2003 4:29 pm
Posts: 14
Location: Toronto, Canada
A couple of things. Someone correct me if I'm wrong, but unless you are using the new transactional tables in MySQL, then you can sit there all day and rollback and nothing will happen.

Also, if using the Transaction API, you do not need to use session.flush(). Just call tx.commit() when ready to save.

As for the expected behaviour of tx.rollback(), again someone please correct me if I'm wrong, but you already saved the object, so what you are rolling back from?

John


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 30, 2003 5:57 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
MySQL requres the InnoDB table subsystem for transactions to work correctly.

It is true the tx.commit has an implied session.flush() so it is not required to flush before a tx.commit() but there are times when you might want to flush but not commit the transaction straight away. Note: Don't flush unless its necessary as it can have a performance penalty.

The session.save() sets up the sql operation, session.flush() sends the sql operation to the database, and finally tx.commit causes the change to be permanent.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 30, 2003 7:02 pm 
Newbie

Joined: Wed Aug 27, 2003 9:08 pm
Posts: 5
Silly me. I forgot about this MySQL InnoDB type. Now the rollback works perfectly once I use InnoDB tables for testing. I usually don


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 30, 2003 9:08 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
No problem,

Off topic but there are a few brisbane based hibernate users. Maybe we should get together for a beer as its getting hot in brisbane here now. Temp of 35 and no air conn is not the best conditions to do any thinking.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 30, 2003 9:42 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 8:07 pm
Posts: 229
Location: Brisbane, Australia
Brisbane: Hibernate capital of the world :)

Seriously, I would be totally up for that. Maybe we could organise it in a miscellaneous forum thread?

P.S. Why the #$%@ don't you have air conditioning? Are you MAD? :)


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