xytec
xytec

Reputation: 65

NHibernate: Is it okay to leave using(var tx = session.BeginTransaction()){} without Commit() during read?

Quick question.. In a function that checks if a certain Terminal Id is available, is it okay for me to do it as below?

using (var tx = session.BeginTransaction())
{
    return ((new TerminalDAO(sm.Session)).Get(tid) == null) ? true : false;
}

Or is it advisable to do it with the Commit()?

Terminal terminal = null;
using (var tx = session.BeginTransaction())
{
    terminal = (new TerminalDAO(session)).Get(tid);
    tx.Commit();
}
return (terminal == null) ? true : false;

Upvotes: 4

Views: 239

Answers (1)

Radim Köhler
Radim Köhler

Reputation: 123901

For readonly operations - I would firstly appreciate you, that you did included transaction even for readonly operation - and also suggest to use a rollback and in fact the explicit rollback.

Please check these:

My reason for explicit Rollback() would be:

  1. Why rollback? We know it is READ, any accidental WRITE is not intended
  2. Why explicit? Anyone coming later can see it. Self-describing code is the way. Relying on defaults could later lead to unexpected behaviour (e.g. vendor changed the defaults)

Upvotes: 5

Related Questions