-->
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.  [ 7 posts ] 
Author Message
 Post subject: hibernate query reagrding transactions and session
PostPosted: Tue Jun 06, 2006 4:34 am 
Beginner
Beginner

Joined: Mon Mar 14, 2005 9:07 am
Posts: 27
Hi ,

I am using Hibernate 3 and have a small query regarding hibernate session.

If suppose I have an Object called 'Order' containing 'Buyer' and 'Seller' references like

Code:
class Order{
Buyer b;
Seller s;
}


when I try to save order object like session.save(order) it will try to persist the Buyer and Seller details in their respective tables. Now while trying to persist Order if after saving the buyer details, and error comes up while trying to save the Seller details, would the Buyer details be also reverted back or do I have to use session.beginTransaction() in order to ensure atomicity before doing session.save(order);


Thanks ,
Ashish Abrol


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 06, 2006 5:58 am 
Expert
Expert

Joined: Thu May 26, 2005 9:19 am
Posts: 262
Location: Oak Creek, WI
Hi,

When calling session.save(order), the values are inserted but not commited.
So rollback the transaction if you get an error.
tx.rollback();

If you don't get any errors then tx.commit()

_________________
RamnathN
Senior Software Engineer
http://www.linkedin.com/in/ramnathn
Don't forget to rate.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 06, 2006 6:05 am 
Beginner
Beginner

Joined: Mon Mar 14, 2005 9:07 am
Posts: 27
Thanks for the prompt reply but what I wanted to ask was that just using session.save would do that or do I have to use session.beginTransaction() method.

I agree with what you said that the values are inserted but not commited when we do session.save(order) but if there is any error then Hibernate would do that on its own or do we have to use transaction alongwith that.

I have many methods where I am only using session.save with using transactions. What I need to know is that while saving the order object if there is any error while saving any of the references will Hiberante revert back if I am not using transactions.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 06, 2006 6:17 am 
Expert
Expert

Joined: Thu May 26, 2005 9:19 am
Posts: 262
Location: Oak Creek, WI
Hi,

Do look into the session methods.

http://www.hibernate.org/hib_docs/v3/ap ... ssion.html

I dont see any rollback in the session. we flush the session to save.

Possibly will investige and then let you know.

_________________
RamnathN
Senior Software Engineer
http://www.linkedin.com/in/ramnathn
Don't forget to rate.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 06, 2006 6:33 am 
Beginner
Beginner

Joined: Mon Mar 14, 2005 9:07 am
Posts: 27
I think I failed to explain my problem properly. I'll try to do that again.

These are the steps-

1)Session s = HibernateUtil.currentSession();
2)s.save(order);

At this stage Buyer gets inserted but while trying to insert Seller there is some error. So it is obvious that Seller would not be inserted in the Seller table.

Now will the Buyer details that have already been inserted, would those details be also deleted or revert back.

Remember that I am NOT USING TRANSACTIONS anywhere in the above example. ie. There is no s.beginTransaction(); or rollback code above.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 06, 2006 9:05 am 
Beginner
Beginner

Joined: Thu Sep 01, 2005 7:43 am
Posts: 31
Location: León (Spain)
Every operation done with a session has an implicit transaction associated (even if you didn't create one yourself).

From the Session API Documentation (http://www.hibernate.org/hib_docs/v3/ap ... ssion.html):

"If the Session throws an exception, the transaction must be rolled back and the session discarded. The internal state of the Session might not be consistent with the database after the exception occurs."

From that phrase, I understand that you should call Session.getTransaction().rollback(), because your Session might have wrong data in it.

Hope it helps.

Pd: and answering your question, the buyer details should be still in the session, in spite of the exception.

_________________
Please rate...

Expert on Hibernate errors... I've had them all... :P


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 06, 2006 7:21 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 8:07 pm
Posts: 229
Location: Brisbane, Australia
You should have some kind of transaction management strategy in place.

Whether you do it programmatically or have some infrastructure (JTA-type setup) do it for you, you should plan out what you *want* to happen.

For example, doing it programmatically, if you want the entire operation to be atomic, then do this:

1)Session s = HibernateUtil.currentSession();
2)Transaction tx = s.beginTransaction();
3)s.save(order);
4)tx.commit();

If you get an error on line (3) then do a tx.rollback().

Now you don't have to ask anyone or wonder what Hibernate is doing.

_________________
Cheers,
Shorn.


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