-->
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.  [ 9 posts ] 
Author Message
 Post subject: Is opening a transaction necessary for select?
PostPosted: Fri Jul 30, 2004 2:31 am 
Newbie

Joined: Mon Jun 14, 2004 5:16 pm
Posts: 16
Location: New York
Is it necessary to create a new transaction for select queries like
Code:
MyClass entity = (MyClass) session.get(MyClass.class, id)


as commit and/or rollback does not make sense in this context...

Thanks,
Cagan


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 30, 2004 4:22 am 
Senior
Senior

Joined: Sun Mar 14, 2004 10:16 am
Posts: 129
Location: Ankara
No

_________________
-developer


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 30, 2004 4:22 am 
Senior
Senior

Joined: Sun Mar 14, 2004 10:16 am
Posts: 129
Location: Ankara
No

_________________
-developer


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 30, 2004 4:44 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Yes! Always do explicit transaction management! This has been discussed numerous times alredy in the forum.


Top
 Profile  
 
 Post subject: rollback
PostPosted: Fri Jul 30, 2004 12:02 pm 
Newbie

Joined: Mon Jun 14, 2004 5:16 pm
Posts: 16
Location: New York
In case of an HibernateException during the select with tx management, are tx.commit and tx.rollback necessary?

so is it:

Code:
try{
   session  = get current session
   tx = session.beginTransaction()
    return (MyObject) session.get(MyObject.class, id);
}catch(HibernateException) he){
...
}finally{
    close the session
}



or
Code:
try{
   session  = get current session
   tx = session.beginTransaction()
   MyObject obj = (MyObject)session.get(MYObject.class, id);
   tx.commit();
   return obj;
}catch(HibernateException) he){
  try{
     tx.rollback();
  }catch(HibernateException he){
  }
}finally{
    close the session
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 30, 2004 12:04 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Why shouldn't they be required?[/url]

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 30, 2004 12:04 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
I think it is good style to do transaction handling just like in every other case. Commit if everything is fine, rollback if there is an exception.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 02, 2004 9:15 am 
Proxool Developer
Proxool Developer

Joined: Tue Aug 26, 2003 10:42 am
Posts: 373
Location: Belgium
Yes!

Besides all other reasons already mentionned in the thread (and others), one can add the following:

If not protected by a transaction you may be impacted by changes performed by other threads (cfr. transaction isolation level). Think about it...


Top
 Profile  
 
 Post subject: thanks
PostPosted: Mon Aug 02, 2004 6:52 pm 
Newbie

Joined: Mon Jun 14, 2004 5:16 pm
Posts: 16
Location: New York
Got the point..Thanks.


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