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: Why wrap Queries in transactions?
PostPosted: Tue Sep 19, 2006 2:10 pm 
Newbie

Joined: Tue Sep 19, 2006 2:04 pm
Posts: 3
Rank newbie question here.
Why are simple queries in the beginning examples always wrapped in a transaction. That seems wasteful. I could understand transactions for updates/inserts but queires? what's up with that?

for example:
Code:
Transaction t = session.beginTransaction();
List things =
      newSession.find("from SomeTable where something");


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 19, 2006 3:57 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
I don't know. I sure as hell don't wrap my hibernate queries in transactions though.

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 19, 2006 5:13 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Quote:
I don't know. I sure as hell don't wrap my hibernate queries in transactions though


So you are running your queries with dozens of short auto-committed transactions. Why exactly is this better than one? Note that if you say something like "it is faster" or "it scales better" or "it makes my code cleaner" you actually have to prove it.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 19, 2006 5:26 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
I was speaking as a general observation of my code.

I have many transactions, just don't usually use them when I'm calling a one-time use query that is kinda by itself.

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 19, 2006 5:32 pm 
Newbie

Joined: Tue Sep 19, 2006 2:04 pm
Posts: 3
christian wrote:
Quote:
I don't know. I sure as hell don't wrap my hibernate queries in transactions though


So you are running your queries with dozens of short auto-committed transactions. Why exactly is this better than one? Note that if you say something like "it is faster" or "it scales better" or "it makes my code cleaner" you actually have to prove it.


That might be the answer to my question. What "short auto committed transactions"? Simply doing a select causes hibernate to create a short auto committed transaction"?

If that is true then if you do a single select you don't need to include the beginTransaction call since it's auto but if you do more than one select and you want them in the same tranactions (but how do you roll back a select?) then you use your transaction facade class.

I may be getting more confused.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 19, 2006 6:04 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Quote:
Simply doing a select causes hibernate to create a short auto committed transaction"?


Of course, because there is simply no way to talk to a database "outside" or "wihtout" a database transaction at the lower level. The JDBC connection Hibernate obtains will be in whatever mode it is by default. (Actually, the behavior is pretty much undefined if you do not enable auto commit handling in the Hibernate configuration, in that case.)

And yes, the auto-commit mode is evil and confusing.

Just use these Session/transaction handling patterns for all units of work and do not use auto-commit mode.

http://hibernate.org/42.html

I have explained this again in much much more detail (it is not really a Hibernate thing) in the book Java Persistence with Hibernate, which you can subscribe to at http://www.manning.com


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 20, 2006 8:58 am 
Newbie

Joined: Tue Sep 19, 2006 2:04 pm
Posts: 3
Thanks for the reply and clarification. This was inadequately addressed in the first edition books I've read. I assume you are plugging the new edition because you clarified this in print.

on the page you cited:
... in fact, every SQL statement, be it queries or DML, has to execute inside a database transaction. There can be no communication with a database outside of a database transaction.

about auto commit. Hibernate automatically turns off autocommit?
That would make sense with the preceding paragraph.


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.