I'm trying to use LockMode to make sure the entity I get through my session locks the row(s) in the database. However, the generated SQL (for MS SQL Server) appears to only be locking the base row. This leads to deadlocks when NHibernate eventually updates the properties in the subclass mapping to the child table's row.
I fetch the entity through the session like this:
CustomerOrder myCustomerOrder = mySession.Get<CustomerOrder>(myCustomerOrderId, LockMode.Upgrade);
Customer order is a subclass of the Order base class, mapped with table-per-subclass:
class Order {} // tblOrders
class CustomerOrder : Order {} // tblCustomerOrders
The generated SQL (observed via NHProf) is:
SELECT ...
FROM dbo.tblOrders customeror0_ with (updlock, rowlock)
inner join dbo.tblCustomerOrders customeror0_1_
on customeror0_.OrderID = customeror0_1_.OrderIDID
What I *think* I need is for the "(updlock, rowlock) to also follow tblCustomerOrders (here's one source: http://stackoverflow.com/questions/2577337/can-i-lock-a-record-from-a-join-sql-statment-using-rowlock-updlock).
So if that is what is needed so that the *entity* is locked, shouldn't NHibernate be doing this?
Here's an example of the deadlock: