Results 1 to 2 of 2
  1. #1
    sfaisalawan is offline Member
    Join Date
    May 2011
    Rep Power

    Default Record Lock in Java

    Hi All

    I have question on Record Lock in Java.

    I have this Situation.
    In my java application, a user can login and search all the (some product )cases which are open.
    He opens up this case, see the details and click on EDIT button to edit the case.

    At the same time another user logins and tries to edit or delete.
    Then a message should pop up saying, the record is locked.

    How do i lock the record for one particular user, and unlock after he is done updating ?

    i am using microsoft access database


  2. #2
    shnplr is offline Member
    Join Date
    May 2011
    Rep Power



    This is more a design issue and solutions can depend on many things including "what is the actual problem your trying to solve". For example a common approach is optimistic locking where nothing is physically locked, however the update fails if data was changed by another user since last read - this is basically done by adding a TRANSACTION_ID column and incrementing the value e.g.
    Java Code:
    int rows = update PRODUCT set <your values>..., TRANSACTION_ID = obj.transactionId + 1 
    where PRODUCT_ID = :product.productId and TRANSACTION_ID = :product.transactionId
    if (rows == 0) {
    throw new RecordChangedException();
    If no rows were updated this means someone else updated the record (and changed the transaction_id). What you do in this case is up to you - for example you could possibly try to "merge" this users changes with the current changes, so the 2nd user can review the changes and try update again - on the other hand if its not too big a deal, or the contention is infrequent you could simply throw away the changes and make the user re-enter again.

    You may be able to lock the physical database row by doing a "select ... for update", but this might mean noone else can even read the row.

    If you try to lock using a java construct running on your server, you could run into problems if your application was deployed on multiple servers.

    Perhaps you could add a new column on your table, say called LOCK_USER (if your user can only logon once) or LOCK_ID (if your user can logon multiple times and you can create a unique id for each logon session).

    The when user clicks edit button and you go back to the database to fetch latest data you could do something like:

    Java Code:
    // first try to lock the row
    int rows = update PRODUCT set LOCK_ID = :lock_id 
    where PRODUCT_ID = :product.productId and LOCK_ID is null;
    if (rows == 0) {
    throw new RecordLockException();
    // lock update successful - get the product data to edit
    int rows = select * from PRODUCT 
    where PRODUCT_ID = :product.productId and LOCK_ID = :lock_id
    // add additional test if not participating in a transaction...
    if (rows == 0) {
    throw new RecordLockException();
    then when your user updates you could do something like:

    Java Code:
    int rows = update PRODUCT set ...(all your values), LOCK_ID = null
    where PRODUCT_ID = :product.productId and LOCK_ID = :lock_id
    if your user clicks cancel you could clear the lock

    Java Code:
    update PRODUCT set LOCK_ID = null 
    where PRODUCT_ID = :product.productId and LOCK_ID = :lock_id
    You should also consider what happens if the user locks the record and then closes the client or browser, or simply goes to lunch and the record remains locked.

    Hope this gives you some ideas/thoughts.
    Last edited by shnplr; 05-08-2011 at 10:03 AM. Reason: add CODE tags

Similar Threads

  1. Replies: 2
    Last Post: 07-26-2011, 05:53 PM
  2. why this simple java application goes into dead lock?
    By jokop in forum Threads and Synchronization
    Replies: 12
    Last Post: 02-04-2011, 09:57 AM
  3. lock desktop using java code
    By meghana in forum Forum Guides
    Replies: 3
    Last Post: 03-30-2010, 07:24 AM
  4. How to reserve/lock a record/object for update
    By Steve11235 in forum Java Tip
    Replies: 0
    Last Post: 01-12-2009, 04:46 PM
  5. Replies: 0
    Last Post: 09-28-2007, 02:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts