views:

350

answers:

2

SQL Server doesn't support the "SELECT FOR UPDATE" syntax, which is used by NHibernate for pessimistic locking. I read here on StackOverflow descriptions of other alternatives (I liked the "SELECT WITH (...)" because it's quite close).

however NHibernate doesn't seem to support this syntax.

do you have a workaround for this? I believe this can be achieved by tinkering with NHibernate internals, but that's not cost effective for me at the moment (learning curve). I could also perhaps use a stored procedure with application locks, and access that from NHibernate. any other suggestions? (aside from always reading before writing...)

A: 

I don;t know much about NHibernate, but in SQL columns of ROWVERSION type are commonly used to implement optimistic locking. Example here

AlexKuznetsov
+1  A: 

NHibernate has a version strategy for optimistic locking:

http://ayende.com/Blog/archive/2009/04/15/nhibernate-mapping-concurrency.aspx

Daniel Auger
thanks, Ayende's blog is great and although I mistakenly asked about optimistic locking (although I need pessimistic lock), this post helped me - since it demonstrated how a session.Get<Person>(1,LockMode.Upgrade); translated to ... FROM ... WITH (updlock, rowlock) - which means NHibernate (at least 2.x) DOES support SQL Server's pessimistic locking syntax.
Yonatan Karni