-->
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 Question
PostPosted: Thu Sep 30, 2010 3:37 pm 
Newbie

Joined: Thu Sep 30, 2010 3:29 pm
Posts: 3
New to Hibernate here. Confused with the transaction...
Is there any difference b/w
begin a transaction, select, update
and select, begin transaction, update?

Code:
Session newSession = getSessionFactory().openSession();
Transaction newTransaction = newSession.beginTransaction();
SomeObject obj = newSession.load....
obj.setName("WHATEVER");
newSession.saveOrUpdate(obj);
newTransaction.commit();
newSession.close();


Code:
Session newSession = getSessionFactory().openSession();
SomeObject obj = newSession.load....
Transaction newTransaction = newSession.beginTransaction();
obj.setName("WHATEVER");
newSession.saveOrUpdate(obj);
newTransaction.commit();
newSession.close();


Thanks!


Top
 Profile  
 
 Post subject: Re: Transaction Question
PostPosted: Thu Sep 30, 2010 7:04 pm 
Beginner
Beginner

Joined: Fri Mar 11, 2005 7:46 am
Posts: 29
Correct me if I'm wrong - I think Transaction means "unit of work" - either everything is done, or nothing. So, because SELECT does not change any data, in this example there is no difference between these 2 options. More often you will have several data changing operations, eg. UPDATE and DELETE united within a transaction.


Top
 Profile  
 
 Post subject: Re: Transaction Question
PostPosted: Thu Sep 30, 2010 8:58 pm 
Newbie

Joined: Thu Sep 30, 2010 3:29 pm
Posts: 3
Thank you for your reply.
I am new to Hibernate and I thought they are the same.
But I got DB lock wait timeout a lot lately, and I'm trying to figure it out what I did wrong in the code.
The transactions are not big when updating or inserting, but there are constantly a lot of SELECT on every request on the same table.
I am not sure if high volumn of SELECT with occasional UPDATE or INSERT would lock the table.


Top
 Profile  
 
 Post subject: Re: Transaction Question
PostPosted: Thu Sep 30, 2010 10:15 pm 
Beginner
Beginner

Joined: Fri Mar 11, 2005 7:46 am
Posts: 29
Some databases have SELECT locks too, this might be your case. I would suggest using SELECT outside transaction. YMMV tho


Top
 Profile  
 
 Post subject: Re: Transaction Question
PostPosted: Fri Oct 01, 2010 6:48 am 
Newbie

Joined: Tue Feb 02, 2010 10:37 am
Posts: 13
In case of "Select" being used before "Update" in your cases (i) and (ii) doesn't make a difference.However it might be different if u'r select statement follows an update statement and both update & select accesses the same table/resultset.


Top
 Profile  
 
 Post subject: Re: Transaction Question
PostPosted: Fri Oct 01, 2010 5:34 pm 
Newbie

Joined: Thu Sep 30, 2010 3:29 pm
Posts: 3
Thanks for the reply. Can you please tell me what the difference?


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.