Google



Not all movies are my choice. The wife has some say too! :)
Sun Mon Tue Wed Thu Fri Sat
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            







My Amazon.com Wish List

Locking Database Records

How do you go about locking database records in a web application app? Say User 1 selects a record to edit and User 2 selects that same record a few seconds later, how do you deny User 2? Update a field in the database? And say User 1 is on the edit screen for 20 minutes, made a couple of changes, but never clicked submit? Do you time them out? Do you save the record then time them out? Would ajax work here? Any suggestions on how one might due this? (Using Coldfusion 8 and MySQL 5)

(Boy, that's a lot of questions)

Comments
Adrian J. Moreno's Gravatar I'll post a more thorough rundown on my site, but here's the basic idea:

Add a "lock" flag (column) to each table that can be edited by more than one user. Also add a "edit started" timestamp column.

When someone clicks on "edit", set that record's flag to "1" if and only if its current value is 0. Now let that person access the edit form.

If the flag is already set to 1, throw up a notice that someone else is editing that record.

When the 1st person submits the change, flip the flag back to 0.

Person 2 can now edit the record.

To address the "stepped away" issue, run a scheduled task every 30 minutes or so. Look for records with a flag = 1 and an edit start time > 30 minutes. Adjust the timing as you like.

I did this with an application for US Airways and it worked out very well for all involved.
# Posted By Adrian J. Moreno | 5/19/08 5:54 PM
Chris's Gravatar That was kind of what I was thinking, but I was worried about the step away. I guess that is the only way to really do it. Thanks.
# Posted By Chris | 5/19/08 6:07 PM
Magnus Thyvold's Gravatar I haven't tried this but I don't see why you couldn't use AJAX. On edit pages you could have an AJAX function touch base with the server every 5, 10, 30 seconds (what ever seems the right balance). If more than a few of these are missed consecutively, the flag it reset and the record released. The user would have to move off the edit pages to lose their lock. You would probably still want to run a clean up routine like above as a backup.
# Posted By Magnus Thyvold | 5/19/08 8:48 PM
Matt Woodward's Gravatar Use LiveCycle DS. :-)
# Posted By Matt Woodward | 5/19/08 10:47 PM
Chris's Gravatar @Magnus, I'll have to take a look at that.

@Matt, Do you know of any examples?
# Posted By Chris | 5/19/08 11:20 PM
Chris's Gravatar @Adrian, One thing I was thinking of is what if the browser crashes. That means the record would be locked for the 30 minutes or so. I guess I could do an if statement saying if the username equals that of the user trying to get in, let them back in. Any drawbacks to that?
# Posted By Chris | 5/21/08 3:37 PM
Adrian J. Moreno's Gravatar @Chris: checking usernames would work just fine. Also, you would want to have an Admin user that could go in an unlock individual records or run the bulk unlock whenever they choose.
# Posted By Adrian J. Moreno | 5/21/08 6:57 PM
Tony Garcia's Gravatar Here's something you might want to check out. This guy takes a different approach to record locking:

"Unlike database solutions that store a flag in a record LockMonger works by storing information in an application-scoped array, so there is zero chance that two users can collide."

http://mysecretbase.com/lockmonger.cfm
# Posted By Tony Garcia | 6/18/08 5:25 PM