V
V
Vladimir Borisyuk2018-11-10 11:34:53
MySQL
Vladimir Borisyuk, 2018-11-10 11:34:53

How to block concurrent editing of record in db on laravel?

Good afternoon! How to implement a ban on simultaneous editing of a record in a MySQL database using LARAVEL? That is, if one user edits a record, then another should be given a message that the record is currently locked. It is also necessary to provide for the situation that the user will not save the changes, but simply close the browser. Then the lock must also be removed! Admins can work both on a PC and on a mobile device (and android browsers do not track tab closing).

Answer the question

In order to leave comments, you need to log in

4 answer(s)
V
Vladimir S, 2018-11-11
@hePPer

you can try to do without blocking if the situation is not very frequent.
give several users the opportunity to get a record for modification, while adding the field TimeChange to the table - where to enter the time of the last modification of the row. when making changes to the database, check the key of the updated record and the modification time (update .... where id=1 and lastEdit='lasteditdate'). if the modification time does not match, then inform the user that the record he is editing has already been changed by someone and show him the new changes.

V
Vladimir Borisyuk, 2018-11-11
@Flight404

After reading the comments and answers, I realized that I do not know and do not know how to use a websocket. And I made this option. The database has a locked_at field, where the editing time is stored, and a locked_by field, where the user id is stored. Accordingly, if other users try to edit or delete a record, then there is a check in the database; if the current time is more than 1 minute of time in locked_at, then this can be done. And as long as the user edits the record, there is an ajax request every 30s, which updates the locked_at field. And accordingly, when the tab is closed or saved, then ajax does not fly and after 1 minute the first one to reach will get the opportunity to edit.

D
Dmitry, 2018-11-15
@dlnsk

There are two ideologically different implementation options:
1. The main one is the one who first opened the editing form (you described this option above). Requires additional fields like locked_at, ajax, etc.
2. The main one is the one who first changed. Two users open the form, edit. The first saves without problems, and the second , when saving , receives a message that the record has already been changed earlier. You can show him diff or something else ... (this option was described by Vladimir S). There are no extras here. fields are not needed, just check before saving that the updated_at field has not changed.

E
Eugene Wolf, 2018-11-10
@Wolfnsex

How to implement a ban on simultaneous editing of a record in a MySQL database using LARAVEL?
Add a flag (a column in the database), a similar functionality was invented and has been used since the shaggy years, respectively. when trying to start editing a post, this flag must be checked.
A slightly more complicated option is to block the entry at the database level, I don’t know how it suits you and whether you need it at all, but there is such an option too.
And in android browsers, the closing of the tab is not tracked !!!
I'll tell you a secret, closing a tab even in browsers "not android" can not always be tracked and I would not completely rely on something like that. One of the options for "reliable" tracking of the presence of a "client on the line" looks something like this:
1. Raise the web socket server, it will also control the blocking of a particular record in the database
2. Every N-seconds send a PING request to the client if the client did not answer (X times in a row, and / or within Y seconds), reset the block (well, you can also try to send a notification to the client, or hang a connection activity indicator directly on the client in the browser). Something similar is used by many network services, such as IRC servers/clients.
PS These are the simplest and most effective options. They are not directly related to Laravel and, given the context of the question, they should not.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question