Results 1 to 1 of 1
- 01-12-2009, 03: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.
Similar Threads
-
Record Lock in Java
By sfaisalawan in forum JDBCReplies: 1Last Post: 05-08-2011, 08:10 AM -
lock on ".class" object
By rajinder5 in forum Threads and SynchronizationReplies: 0Last Post: 10-11-2010, 04:38 PM -
javacould not reserve enough space for object heap
By Camden in forum New To JavaReplies: 0Last Post: 08-08-2008, 02:56 AM -
Update a record in Random access file
By Rgfirefly24 in forum New To JavaReplies: 2Last Post: 04-24-2008, 10:07 PM -
object lock question
By simon in forum New To JavaReplies: 2Last Post: 08-01-2007, 04:36 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks