R
R
Rukis2017-09-27 17:08:54
MySQL
Rukis, 2017-09-27 17:08:54

How to protect yourself from situations of simultaneous data editing by different users?

There is a multi-user site where several users can edit articles at once.
How to protect yourself from a situation where several users (or one user, but in different tabs) open the editor of one article and accidentally overwrite changes made by each other?

Answer the question

In order to leave comments, you need to log in

6 answer(s)
A
Anton Anton, 2017-09-27
@Fragster

Enter an additional field "version", and increase it when writing. Well, before recording, check that the version matches the one being edited. This is called "optimistic locking".

A
Alexey Ukolov, 2017-09-27
@alexey-m-ukolov

- Lock entities when someone opens edit.
- Display a warning.
- As a result, show the diff and the conflict resolution tool.

D
Dmitry Evgrafovich, 2017-09-27
@Tantacula

Add a "editable" flag. When opening, check it and display a warning, remove it when saving. You can also add the id of the person who edits.

N
Nikolai Konyukhov, 2017-09-27
@heahoh

Semaphores, for example

A
Alexander Ter, 2017-09-27
@alexsandr0000

Add a sign of blocking of writing and reading. And when editing, set 1, and when closing 0. And it is desirable to do this atomically or through a double lock, for example (you can read it here ).

R
res2001, 2017-09-27
@res2001

Usually, in this case, the simplest strategy works - whoever wrote it down last, well done.
In general SQL Server handles this sort of thing and usually you don't have to do anything.
If you still want to do it, then you need to use "manual" locks, but there is always a risk of not removing the lock. This is often worse than using the default strategy.
Theoretically, before writing, you can check if this record has changed, if it has not changed, then change it, if it has changed - an error. But this must be done in one transaction.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question