This forum is now a read-only archive. All commenting, posting, registration services have been turned off. Those needing community support and/or wanting to ask questions should refer to the Tag/Forum map, and to http://spring.io/questions for a curated list of stackoverflow tags that Pivotal engineers, and the community, monitor.

Conceptual Question Locking and Transactions Webapp

Dec 19th, 2011, 05:02 AM

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.

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 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.

Comment

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.