Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET NHibernate transaction duration
    text
    copied!<p>Currently in our ASP.NET app we have 1 session per Request, and create one transaction every time we load or update and object. See below:</p> <pre><code> public static T FindById&lt;T&gt;(object id) { ISession session = NHibernateHelper.GetCurrentSession(); ITransaction tx = session.BeginTransaction(); try { obj = session.Get&lt;T&gt;(id); tx.Commit(); } catch { session.Close(); throw; } finally { tx.Dispose(); } return obj; } public virtual void Save() { ISession session = NHibernateHelper.GetCurrentSession(); ITransaction transaction = session.BeginTransaction(); try { if (!IsPersisted) { session.Save(this); } else { session.SaveOrUpdateCopy(this); } transaction.Commit(); } catch (HibernateException) { if (transaction != null) { transaction.Rollback(); } if (session.IsOpen) { session.Close(); } throw; } finally { transaction.Dispose(); } } </code></pre> <p>Obviously this isn't ideal as it means you create a new connection to the database every time you load or save an object, which incurs performance overhead. </p> <p>Questions:</p> <ul> <li>If an entity is already loaded in the 1st level cache will the GetTransaction() call open a database connection? I suspect it will... </li> <li>Is there a better way to handle our transaction management so there are less transactions and therefore less database connections?</li> </ul> <p>Unfortunately the app code is probably too mature to structure everything like so (with the get and update all in the same transaction):</p> <pre><code>using(var session = sessionFactory.OpenSession()) using(var tx = session.BeginTransaction()) { var post = session.Get&lt;Post&gt;(1); // do something with post tx.Commit(); } </code></pre> <p>Would it be a terrible idea to create one transaction per Request and commit it at the end of the request? I guess the downside is that it ties up one database connection while non-database operations take place.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload