How to handle this concurrency script using NHibernate + asp.net mvc?

Context: a web application written in asp.net MVC + NHibernate. This is a card game in which players play simultaneously, so their actions can simultaneously change the same field of an object (they all do x = x + 1 in the field). It seems pretty classic, but I don't know how to handle this.

Needless to say, I cannot present the user with a pop-up window that says: "The entity has been changed by another player. Merge or cancel?". When you think that this is due to the action on the card, I cannot interfere with it. My application must internally handle this script. Since the field is in the entity class, and each session has its own instance of the object, I cannot just take a CLR lock. Does this mean that I have to use pessimistic concurrency so that every web request acting on this object is queued until the player has finished its action? In practical terms, does it mean that every request in PlayCard should use a lock?

Please do not send me to NH doc about concurrency or the like. I use a method that should be used in this case, not how to implement it in NH.

thank

+3
source share
2 answers

You can try to apply optimistic locking as follows:

The DB entity will have a version of the column tracking object ( nhibernate.info link ).

If you get an “outdated version” exception when saving an object (= changed by another user), reload the object and try again. Then send the updated value to the client.

, back-end , , , . , .

, , .

, , , SELECT FOR UPDATE ( LockMode.Upgrade NH Get). , SQL Server: SO > .

concurrency , . UI, , , , , .

+1

- . , . factory, factory . Nh , , factory, , , , . , - , . concurrency . , concurrency DB NH, . , -.

+1

All Articles