Announcement Announcement Module
Collapse
No announcement yet.
Conceptual Question Locking and Transactions Webapp Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Conceptual Question Locking and Transactions Webapp

    Hi Guys,

    I do have a conceptual question and don't know what to google/search for as I don't have any approach yet. Let me try to explain my problem by example.

    Imagine a full flavoured web application, using a database, hibernate for jpa, spring mvc, and a javascript/ajax/jsp frontend. Let's suppose the app is supposed to be a multi-user capable address book. If a user opens an address book entry in the browser the data get's fetched via ajax from a spring controller via service methods and daos. It's displayed in the browser such that a user can edit the record.

    Now imagine user Bob opens the record "Alice Smith, Park Avenue" on Monday. He leaves the browser open (let's also assume that there is no session expireing etc. Now he goes camping till Wednesday.

    Code:
    State after the operation:
    Databse: "Alice Smith, Park Avenue"
    Bob's Browser:  "Alice Smith, Park Avenue"

    In the meantime Charlie opens the record "Alice Smith, Park Avenue" on Tuesday and updates to "Alice Smith, 45 Park Avenue, NY". The record gets update in the database.

    Code:
    State after the operation:
    Database: "Alice Smith, 45 Park Avenue, NY"
    Bob's Browser:  "Alice Smith, Park Avenue"
    Charlie's Browser: "Alice Smith, 45 Park Avenue, NY"
    Now Bob returns on Thursday from camping and finds his browser open presenting the record "Alice Smith, Park Avenue" as it was loaded on Monday. He recognizes that Alice's name is not complete and updates the record to "Alice Nicole Smith, Park Avenue". Note that Charlie's update is being overwritten by Bob's submission of the old/expired field data for the street/state.

    Now the state is:

    Code:
    State after the operation:
    Databse: "Alice Nicole Smith, Park Avenue"
    Bob's Browser:  "Alice Nicole Smith, Park Avenue"
    Charlie's Browser: "Alice Smith, 45 Park Avenue, NY"
    Now the question is: How can you prevent this concurrent (dirty?) update. You'd need some kind of locking mechanism, which tells the service methods (and further the frontend) that the record is currently locked by another user and can't be updated. Also you would need the ability to release locks and so on. I don't have a clue which concepts in spring provide that kind of transactionality through to the web frontend. Please advice.

    Thanks,
    Tobi
    Last edited by byte23; Dec 19th, 2011, 06:22 AM.

  • #2
    Optimistic Locking strategy can help you. Try to investigate Optimistic Locking in Hibernate and JPA.

    Comment


    • #3
      In the meantime I found a promising approach. One might tackle this problem by introducing a versioning concept. The @Version annotation helps to manage the state of the record.

      Thus if there is a read and a late commit, the version might have changed inbetween through a concurrent update by another user for instance. Hibernate will then throw an exception as the late write will be recognized by a stale version number of the entity object.

      I am than able to throw this exception through to the frontend and tell the user to refresh his data and retry the update. Or whatever there might be appropriate.

      Thanks all for reading.

      Comment

      Working...
      X