There's rarely a need to.
First off, take a look at:
http://msdn.microsoft.com/library/d...skPerformingOptimisticConcurrencyChecking.asp
and then read this
http://msdn.microsoft.com/library/d...s/cpguide/html/cpconoptimisticconcurrency.asp
As it is probably clear by now, pessimistic locking largely causes more
problems than it is worth and most folks avoid it.
My personal favorite is what is called the "Version Number Approach" in the
first article, because SQL Server supports it natively with the "timestamp"
data type. When you query a row that contains a Timestamp, that column
returns with a long integer, fairly random value. (Not useful for tracking
time, despite it's name). You update your record data in memory. You begin
a transaction. You call an "update" stored procedure and send all of the
fields, including the timestamp, back to it. This proc retrieves the row by
it's primary key and compares the timestamp value. If the value is
different, then the row was updated by someone else. The SP raises an
error, that .NET traps. The .NET app rolls back the transaction. You
inform the user that the data in the row changed, and ask if they want to go
get the original record from the database again (thus keeping the other
person's change) or if they want to simply ignore the other person's change
and overwrite it. If the latter, you call the same stored proc, but pass a
zero for the timestamp value. Your stored proc knows that a zero means
"ignore any changes and overwrite at all costs."
It works. What can I say. I don't use the "overwrite at all costs" method
very often... On one project, where contention was a common, and fairly bad,
thing, I went considerably farther. I would get the changed row, compare it
to the original row (before the user changed it) and for any fields that
were different, create a single pop-up window showing the user the changed
fields and ask if they wanted to apply the changes to their data for the
contended fields. In other words, I helped the user merge their changes.
I hope this helps.
--- Nick