w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
How to prevent lost updates in Java application which uses hibernate?

This sounds like a classic case for an optimistic strategy e.g.

Optimistic concurrency control (OCC) is a concurrency control method for relational database management systems that assumes that multiple transactions can complete without affecting each other, and that therefore transactions can proceed without locking the data resources that they affect. Before committing, each transaction verifies that no other transaction has modified its data. If the check reveals conflicting modifications, the committing transaction rolls back

Perhaps maintaining record version numbers.

e.g. each record would maintain a record version number. When a client receives the client, it would receive the version number too. Upon modification the server compares the record number with that in the database, and if not changed, then a new record is written with an incremented version number.

A second client would then come in with the same record number (i.e. the record is being updated by both clients). This time the server would recognise that the version number has changed, and reject the update.

Note that various implementation options exist. The database can implement this using record numbers or timestamps. The database can maintain a history of edits, or simply the latest version of the record.

© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.