4

I wrote a multi user app in c# some time age using SQL Server 2005 express as back-end.

I have a Orders collection. In order to use this class you would need to instantiate it and just call the Load(CustomerCode) method in order to populate the collection with the specified customers`s orders.

My question:

How do I enforce concurrency, so that only 1 user can request a Orders collection for a specific customer? When the user is done with the object(when the object is set to null), I will need to make it available again.

MegaByte
  • 6,725
  • 10
  • 37
  • 33

1 Answers1

7

You need to implement the Pessimistic Offline Lock pattern.

Essentially you have a table that you put records in that represent a "lock" on records in other tables. When you want to edit a record, you check to see if there's a lock in the lock table first, and react accordingly in your domain logic/UI.

It doesn't have to be a database, it could be an in-memory cache. When I say "table" in my example, I mean a logical table, not necessarily a database one.

Pessimistic Offline Lock prevents conflicts by avoiding them altogether. It forces a business transaction to acquire a lock on a piece of data before it starts to use it, so that, most of the time, once you begin a business transaction you can be pretty sure you'll complete it without being bounced by concurrency control.

Neil Barnwell
  • 41,080
  • 29
  • 148
  • 220
  • Sounds good, but do you perhaps have a link to some code where this has been implemented ? – MegaByte Apr 02 '09 at 14:08
  • Unfortunately I don't have a link to a code sample, but if you have a read of Fowler's book from my link, that is full of code samples. They might even be available for download on the internet - Patterns of Enterprise Application Architecture. – Neil Barnwell Apr 02 '09 at 15:05