3
votes

When doing a Select in nhibernate I suppose a Rollback is faster than a Commit since neither NHibernate nor Oracle has to do any checks for if anything is updated.

But all examples I have found does a Commit after Selecting.
While if I use an implicit transaction the framework does a Rollback.

My question is about speed and resource use, not about architecture or coding stye.

( Now the ones who write nhibernate know way more about the innards of nhibernate than I do and they have chosen to make a Rollback on an implicit transaction with only Select. This might be to totally different reasons than I have (not committing must lead to a rollback being a possible reason). )

( There is also an architectural/psychological argument around what should happen in a possible future edit where someone decides to call an Update in the same code. Should the dev be surprised by a Rollback or should a misplaced Update surprise the users with a Commit? This discussion is not part of the main question. )

using (var session = Helper.SessionFactory.OpenSession())
{
    using (var tran = session.BeginTransaction())
    {
        var ret = session.Query<Person>().Where(x => x.Id == id).Single();
        tran.Rollback();  // <----- or commit?
        return ret;
    }
}

or should I call

tran.Commit();

?

3
Why do you need to use a transaction if you are only performing a single select? - Petter
Someone much more knowledgeable than me suggests it. Resistance is futile - there will be a transaction. - LosManos

3 Answers

3
votes

First off, as you stated in your comment, you should always wrap code using NHibernate in a transaction.

I'm not sure about performance implications of rolling back vs committing, but I've found the idea of a read-only transaction (i.e., a transaction that always gets rolled back at the end of use) to be useful for two main reasons:

  1. Your code clearly indicates that work done inside of the read only transaction will not write anything down to the database, and
  2. You protect yourself from ghost updates caused by bad NHibernate mappings.

It does makes sense intuitively that rolling back a transaction would be a bit faster, but I can't find any (NHibernate) documentation to back that up.

2
votes

Go ahead with committing the transaction, since you started it and there is no error. Even if you use it for select.

Have a look at this too : Should I commit or rollback a read transaction?

0
votes

My knowledge into the ins and outs of DB stuff needs work but the way i thought transactions occured were more like 1 way than 2 way.

Meaning Update, Insert, Delete i am "Setting" the data, this is a transaction that may need a rollback etc.

Selecting data im "Getting" existing data, im not changing the data or effecting its values there for there is no need for a transaction to occur.

As far as i can tell when you refer to "Commit", i cant see that occuring on a select, as i said it appears there isnt a transaction taking place as im simply getting data, not changing its values.

Maybe someone with some concrete facts could advice better.