I have designed database tables (normalised, on an MS SQL server) and created a standalone windows front end for an application that will be used by a handful of users to add and edit information. We will add a web interface to allow searching accross our production area at a later date.
I am concerned that if two users start editing the same record then the last to commit the update would be the 'winner' and important information may be lost. A number of solutions come to mind but I'm not sure if I am going to create a bigger headache.
Are there any better solutions or should I go for one of these?
If you expect infrequent collisions, Optimistic Concurrency is probably your best bet.
Scott Mitchell wrote a comprehensive tutorial on implementing that pattern:
Implementing Optimistic Concurrency
A classic approach is as follows:
when a user starts editing, you do this:
release the lock
when saving the record, set the flag back to false
Another option is to test that the values in the record your changing are the still the same as they where when you started:
SELECT customer_nm, customer_nm AS customer_nm_orig FROM demo_customer WHERE customer_id = @p_customer_id
(display the customer_nm field and the user changes it)
UPDATE demo_customer SET customer_nm = @p_customer_name_new WHERE customer_id = @p_customer_id AND customer_name = @p_customer_nm_old IF @@ROWCOUNT = 0 RAISERROR( 'Update failed: Data changed' );
You don't have to add a new column to your table (and keep it up to date), but you do have to create more verbose SQL statements and pass "new" and "old" fields to the stored procedure.
It also has the advantage that your not locking the records - because we all know that records will end up staying locked when they should not be...
SELECT FOR UPDATE and equivalents are good providing you hold the lock for a microscopic amount of time, but for a macroscopic amount (e.g. the user has the data loaded and hasn't pressed 'save' you should use optimistic concurrency as above. (Which I always think is misnamed - it's more pessimistic than 'last writer wins', which is usually the only other alternative considered.)
@ Mark Harrison : SQL Server does not support that syntax (
SELECT ... FOR UPDATE).
The SQL Server equivalent is the
SELECT statement hint
See SQL Server Books Online for more information.
-first create filed (update time) to store last update record -when any user select record save select time, compare between select time and update time field if( update time) > (select time) that mean another user update this record after select record
The database will do this for you. Look at "select ... for update", which is designed just for this kind of thing. It will give you a write lock on the selected rows, which you can then commit or roll back.
With me, the best way i have a column lastupdate (timetamp datatype). when select and update just compare this value another advance of this solution is that you can use this column to track down the time data has change. I think it is not good if you just create a colum like isLock for check update.