A lost update occurs when two writers read the same state and one overwrites the other without detecting the intervening change. Use optimistic locking when collisions are uncommon and the application can handle conflicts explicitly.
Compare the expected version at write time
In SQL, update using both the record ID and the version the reader observed, and increment that version as part of the same statement. A zero-row update means the condition no longer matches. With Jakarta Persistence, a mapped @Version attribute lets the provider detect a stale entity version.
Handle conflicts deliberately
On conflict, roll back the failed transaction. For an interactive edit, reload the current data and show the conflict. For a bounded automatic retry, start a fresh transaction and recompute the change from the new state. For a simple counter, consider an atomic database increment instead of read-modify-write.
