NHibernate has several concurrency models that you can use:
We will explore each of those in turn.
None basically means that we fall back to the transaction semantics that we use in the database. The database may throw us out, but aside from that, we don’t really care much about things.
Optimistic is more interesting. It basically states that if we detect a change in the entity, we cannot update it. Let us see a simple example of using optimistic dirty checking for changed fields only:
<class name="Person" optimistic-lock="dirty" dynamic-update="true" table="People">
Using this with this code:
using (var session = sessionFactory.OpenSession()) using (var tx = session.BeginTransaction()) { var person = session.Get<Person>(1); person.Name = "other"; tx.Commit(); }
Will result in:
Note that we have so specify dynamic-update to true. This is required because doing so will generally cause much greater number of query plan to exist in the database cache.
Setting optimistic-lock to all would result in:
If the update fails because the row was updated, we will get a StaleObjectException. Like all exceptions, this will make the session ineligible for use, and you would have to create a new session to handle it.
Usually a better strategy is to use an explicit version column. We can do it by specifying <version/>:
<version name="Version" column="Version"/>
And that would result in:
As you can probably guess, if the version doesn’t match, we will get StaleObjectException.
Instead of using numeric values, we can use a timestamp:
<version name="Version" column="Version" type="timestamp"/>
In this case, the property type should be DateTime, and the resulting SQL would be:
This is, of course, a less safe way of doing things, and I recommend that you would use a numeric value instead.
Another option is to use the database facilities to handle that. in MS SQL Server, this is the TimeStamp column, which is a 8 byte binary that is changed any time that the row is updated.
We do this by changing the type of the Version property to byte array, and changing the mapping to:
<version name="Version" generated="always" unsaved-value="null" type="BinaryBlob"> <column name="Version" not-null="false" sql-type="timestamp"/> </version>
Executing the code listed above will result in:
We use the value of the timestamp to ensure that we aren’t overwriting the row data after it was changed. The database will ensure that the row timestamp will change whenever the row itself is updated. This plays well with system where you may need to update the underlying tables outside of NHibernate.
Pessimistic concurrency is also expose with NHibernate, by using the overloads that takes a LockMode. This is done in a database independent way, using each database facilities and syntax.
For example, let us example the following code:
using (var session = sessionFactory.OpenSession()) using (var tx = session.BeginTransaction()) { var person = session.Get<Person>(1,LockMode.Upgrade); person.Name = "other"; tx.Commit(); }
This will result in the following SQL:
We can also issue a separate command to the database to obtain a lock on the row representing the entity:
using (var session = sessionFactory.OpenSession()) using (var tx = session.BeginTransaction()) { var person = session.Get<Person>(1); session.Lock(person, LockMode.Upgrade); person.Name = "other"; tx.Commit(); }
The Get() would generate a standard select, without the locks, but the Lock() method would generate the following SQL:
The behavior for conflict in this case is very simple, we wait. If we wait for too long, the timeout will expire and we will get a timeout exception, because we could not obtain the lock.
That is consistent with how we would use pessimistic concurrency elsewhere.