If that’s what it is, you’d better use it LOCK
. But this mechanism very rarely should be used.
The normal thing is not to want it to be stuck. It can be stuck for hours, days, it is not what you want.
In general you don’t need to do anything. If it depends on data edited in a simple way just let it be recorded. Whoever saves last will have their information fixed. This would happen the same if it was not a competitor, but sequential. Obviously you cannot record what has not been changed. This is a basic care that many people do not, but should do. If this fails the unchanged data will override the changed data by another user and is not what is desired, because it will reverse what the other user did, without being the desired.
There are cases where this is more complicated. If it is a stock quantity for example, it cannot only override the value, because it may have been altered in a way that affected its change. In general this type of data is not simply edited, it is modified in a controlled way by the application. So at the time of recording you have to do a new reading of the data and record the update, it will probably occur very fast, in general below a millisecond and transactional (atomic) form. I’ve seen a lot of damage because the programmer doesn’t do it. Then the solution is to be pessimistic and stop everything. Too much performance problem is because of this.
There are cases that are even more complicated. Other strategies may fit. One of them is to use the LOCK
, but very quickly, not throughout the editing process.
There’s even a way configure transactions with the degree of isolation you want. READ COMMITTED
is the standard and is usually ideal. Some people are tempted to use other levels to be more secure, but this can lock down transactions longer than desired. After all, it’s a kind of lock. Using it correctly will be safe.
It’s easy for people to use these things incorrectly and create a deadlock.
Most people don’t understand the workings of databases. Either they make very simple applications that are independent or have little competition (that doesn’t even occur effectively), or people let problems occur, they are rare. Some even hit by coincidence.
perfect, but for example, if you want to prevent a record being edited from being presented to some other user, then the use of lock would be justified? the use of a column only to represent whether it is in editing or not, is it out of the question? I say this would be a gambiarra?
– Thomas Erich Pimentel
In this rare case, it would be yes. Particularly I would not use another mechanism. But it depends on the case. It may have a justification to do. In everything can be useful if it is well justified. The problem is to do because another has done before and do not understand the reason. Everything you knew why you’re doing, mastering all nuances can hardly be considered gambiarra. Of course, the reason I can’t "I can’t do it any other way", "I don’t have time to do it right", etc. :) It’s possible that a field like this is useful. It is possible that causes problems that the person did not even notice, such as lack of liberation. The
lock
it’s well thought out– Maniero