title: NHibernate: exclusive locking
body: <p>In NHibernate, I want to retrieve an instance, and put an exclusive lock on the record that represents the retrieved entity on the database.</p>
  
  <p>Right now, I have this code:</p>
  
  <pre><code>With.Transaction (session, IsolationLevel.Serializable, delegate
  {
      ICriteria crit = session.CreateCriteria (typeof (TarificationProfile));
  
      crit.SetLockMode (LockMode.Upgrade);
  
      crit.Add (Expression.Eq ("Id", tarificationProfileId));
  
      TarificationProfile profile = crit.UniqueResult&lt;TarificationProfile&gt; ();
  
      nextNumber = profile.AttestCounter;
  
      profile.AttestCounter++;
  
      session.SaveOrUpdate (profile);
  });
  </code></pre>
  
  <p>As you can see, I set the LockMode for this Criteria to 'Upgrade'.
  This issues an SQL statement for SQL Server which uses the <code>updlock</code> and <code>rowlock</code> locking hints:</p>
  
  <pre><code>SELECT ... FROM MyTable with (updlock, rowlock)
  </code></pre>
  
  <p>However, I want to be able to use a real exclusive lock.  That is, prevent that others can read this very same record, until I have released the lock.
  In other words, I want to be able to use an <code>xlock</code> locking hint, instead of an <code>updlock</code>.</p>
  
  <p>I don't know how (or even if) I can achieve that ....  Maybe somebody can give me some hints about this :)</p>
  
  <p>If it is really necessary, I can use the SQLQuery functionality of NHibernate, and write my own SQL Query, but, I'd like to avoid that as much as possible.</p>