-->
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.  [ 4 posts ] 
Author Message
 Post subject: Transactions for Read Only Queries
PostPosted: Mon May 07, 2007 2:07 pm 
Senior
Senior

Joined: Sat Sep 10, 2005 3:46 pm
Posts: 178
I've read other forum posts and emails from the developers' mailing list that indicate it should be a best practice to wrap all data operations within a transaction block or use a form of declarative transaction management. This is suggested for read only queries as well. I have no problem doing this but it does have a performance impact. The performance implications that I see are this:

- 2 small statements issued to the database to begin the transaction and commit the transaction. Granted these are very small statements, but its still sent over the wire to the database as separate communications outside of the actual query statement.

- If the transaction is committed, NHibernate will attempt to flush the session. You can avoid this hit by rolling back the transaction since its a read only query but I find this to be very unintuitive when looking at the codebase. Also rolling back a succesfull query with declaritive transaction management is not very pretty at all.

So in theory, wrapping all data operations within a transaction sounds great, but considering the extra baggage that comes along with it, is it really worth it? Are most devs out there wrapping thier read only operations in a transaction block? I'm very interested in getting some experienced feedback on this.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 09, 2007 1:32 pm 
Senior
Senior

Joined: Sat Sep 10, 2005 3:46 pm
Posts: 178
So am I to assume that nobody is using Transactions for read only queries?


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 09, 2007 2:06 pm 
Regular
Regular

Joined: Wed Jun 21, 2006 3:13 pm
Posts: 110
jnapier wrote:
So am I to assume that nobody is using Transactions for read only queries?


I definately do not.

Code:
public T Get<T>(object id)
{
   return Session.Load<T>(id);
}

public void Save(object o)
{
   BeginTransaction();
   Session.SaveOrUpdate(o);
   CommitTransaction();
}



Now, depending on your situation, if you're hitting SQL 2005 and using System.Transactions then, as long as you're not explicitly opening a transaction in NHibernate, it should stay as a lightweight transaction and not promote to a DTC-managed transaction.

That's probably the biggest performance hit in your scenario, promoting to the transaction manager for your given database platform...


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 09, 2007 4:52 pm 
Senior
Senior

Joined: Sat Sep 10, 2005 3:46 pm
Posts: 178
Thats pretty much what I thought. I assume most devs arent using transactions for read only queries. Yet, Hibernate in Action reccomends that you should use transactions for read-only queries. Actually they have a FAQ on page 159 that asks:

Is it faster to roll back read-only transactions? The book states that they have failed to find any source of real numbers showing a performance difference.

This may be true on the database side, but it isnt true on the Nhibernate side sents a flush wil be performed if the default flush mode is commit.

Performance vs best practices seem to always conflict with each other.


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