In a Spring Boot and Angular invoicing app, two people can open the same invoice and the last save can overwrite the earlier one. I have optimistic locking with a JPA version column, but I'm unsure what the UI should do when a save conflicts. Reload, offer a merge, or show who's editing?
How do you handle two people editing the same invoice?
Answers
Optimistic locking is a good starting point when clashes are uncommon. Send the version that came with the invoice back with the save, and make sure the update checks that version against the database atomically. A mismatch means the client edited an older copy, so return a conflict response rather than silently overwriting anything.
The UI should keep the unsaved edits on screen. Fetch the latest invoice and let the user compare it with their version before deciding what to keep. A plain reload that discards their work would be pretty frustrating.
Reply to this answer
You can make conflicts less annoying by sending only fields the user changed. That gives you a chance to merge edits to different fields, but a version check alone will still reject a stale save.
For a safe merge, compare the original values the user loaded, their changes, and the current values. Apply a changed field automatically only if nobody else changed that same field in the meantime. Show the remaining conflicts for the user to resolve, and protect the final save against another concurrent update too.
Reply to this answer
A database row lock is useful for a short transaction, not a great fit for keeping an edit form open while someone makes coffee. If you need an editing indicator or exclusive editing, treat that as a separate application feature with an expiry and a way to recover abandoned locks. You still need the server to protect the actual save.
Reply to this answer
Another option is to keep invoice revisions instead of overwriting the only copy. Each save can record which revision it started from, so you can detect a stale edit and show the history. That helps with recovery and review, though it doesn't decide which conflicting changes should win.
Reply to this answer
If the goal is genuinely simultaneous editing, rather than just preventing lost saves, that's a different scope. Operational transformation and conflict-free replicated data types are options for collaborative editors, but they're a lot more machinery than most invoice forms need.
Reply to this answer
Add your answer
Answers are reviewed before publication.
_w67~ticketcork.3:
One detail to check with JPA is that the submitted version actually participates in the save. Loading the newest entity and copying fields onto it without checking the client's version can defeat the intended conflict detection.