Consider an application that does something along the lines of:

    var user = session.QueryOver<User>().Where(x =>
x.Name==name).SingleOrDefault();

    if (user == null)
    {
        user = new User(name);
        session.Save(user);
    }

Business rules states that a user's name must be unique, and this is
backed by a `UNIQUE INDEX` in the database. The code above works just
fine, until two users try to register simultaneously with the same
name, both get `user == null` and try to create a new `User`. The
first one to commit will suceed, the second one will fail with an
exception raised from the database.

One way to avoid such race conditions involves wrapping the critical
code in a `lock { }` block, but this will not help when multiple
instances of the applications works against the same database. Now, if
only I could use the locking mechanisms of the RDBS (MS SQL in this
case)…

This is where I got stuck. From what I can read in the NHibernate
docs, I might be able to solve it by adding some a hint to my
`QueryOver()` chain requesting an explicit lock. This would have to be
a table lock, as I have no actual rows yet. Is this possible? How

Alternatively, could I increase the isolation level of the transaction
and gain the required locks automatically? Considering that other (non-
problematic) queries are made within the same unit-of-work, I suspect
this kind of change could introduce a lot of blocking/waiting. Is that
so?

Thanks in advance :)

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to