4

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;
xytec
  • 65
  • 1
  • 1
  • 6

1 Answers1

5

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)
Community
  • 1
  • 1
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
  • Thanks for the response! So the latter is good enough? (after replacing tx.Commit() with tx.RollBack()) – xytec Aug 27 '14 at 06:54
  • 1
    I would never let the transaction without explicit commit or rollback so YES: tx.RollBack() is the way I would go... And the most important reason is: it is visible, when you come after a week, month... you will still see the Rollback.. do not have to think: what is the default here? – Radim Köhler Aug 27 '14 at 06:55
  • Be careful when using this approach in conjunction with second level caching: http://ayende.com/blog/3799/nh-prof-documentation-transaction-and-the-second-level-cache – DanP Sep 09 '14 at 17:29