Results 1 to 1 of 1
- 01-12-2009, 04:46 PM #1
How to reserve/lock a record/object for update
This tip describes a simple method to handle reserving a database record or an object while a user updates it.
The goal of an update reservation is to prevent two users from updating the same information at the same time. If this is allowed, the first user will typically lose their updates. At the same time, a user must not be allowed to lock a record for two hours while they attend a meeting.
My solution is to create a date/time field in the database record or object. In Java, this is more easily represented as a "long", as arithmetic is easier. Here are the rules:
1. If System.currentTimeMillis - lockTime <= maxLockMillis, a current lock exists and a new user cannot lock the record.
2. If System.currentTimeMillis - lockTime <= maxLockMillis, an expired lock exists, and a new user may "steal" the lock by updating lockTime with the current millis.
3. If a user gained a lock, the lock must still exist before the record/object is updated. Note that an expired lock is still valid, regardless of how old it is. However, if another user "stole" the lock, no update is allowed.
4. During the update, the lockTime is reset to 0.
maxLockMillis can be set to any value, and it can vary between tables/objects.
- By sfaisalawan in forum JDBCReplies: 1Last Post: 05-08-2011, 09:10 AM
- By rajinder5 in forum Threads and SynchronizationReplies: 0Last Post: 10-11-2010, 05:38 PM
- By Camden in forum New To JavaReplies: 0Last Post: 08-08-2008, 03:56 AM
- By Rgfirefly24 in forum New To JavaReplies: 2Last Post: 04-24-2008, 11:07 PM
- By simon in forum New To JavaReplies: 2Last Post: 08-01-2007, 05:36 PM