title: How to lock objects before update using NHibernate lock modes?
body: <p>So first let me state what I'm trying to accomplish. I have a table that is populated with jobs. And there is a web service with a method that allows for the alteration of job data called <code>SaveJob</code>. This method retrieves the job and all it's data, runs validations (which require some db queries of their own to other tables) on the new data and then saves it back to the database. It's somewhat slow. About a second.</p>
  
  <p>What happens is occasionally two <code>SaveJob</code> calls will be made close together on the same job and they'll run over each other. Now currently this is the only web server but I would like to have my solution web farm compatible so even though I'm aware of how to solve this issue using a singleton I'd prefer to have the locks handled by the database.</p>
  
  <p>The question is can I use NHibernate and SQL Server database locks to get a second SaveJob call to block when it tries to read a Job that is already being altered by another SaveJob call?</p>
  
  <p>I believe the answer is yes but I'm not really sure how to go about it. I've read the documentation about the <code>ISession.Lock()</code> and I believe what I need is to use the <code>NHibernate.LockMode.Upgrade</code></p>
  
  <p>My next question is when is this lock released?</p>
  
  <p>I assume that it is released when the transaction is committed but I can't find any documentation that explicitly states this.</p>
  
  <p>However if that's the case then is there a way for me to open another transaction and run some queries without closing the transaction I've started when I grabbed the job in the first place? Or do I <em>have</em> to do this all in one transaction?</p>