One of the fundamental algorithms in modern databases is the so-called two-phase locking or 2PL (Two Phase Locking).
It is two-phase, because it was noticed that in order to optimize the taking and unlocking of locks in the database, it is convenient to do this in 2 sessions:
First, we set locks on all resources that need to be read or written to the set of transactions that are currently in the database.
No blocking is released until all the necessary blocks have been set.
This allows transactions to be traversed more efficiently so that they do not wait.
When it starts, there are no other transactions in this model, so the record is started and written. But the second transaction must also read x. This transaction cannot take a read lock on x for the simple reason that x is being written by another transaction at this moment. The line becomes dashed – this means that the transaction is waiting for a lock that was set by transaction t1.
Once transaction t2 has taken all the locks it needs to execute – it still needs a lock on y and a lock on z – only then can it start releasing them. At this moment, the next transaction is unblocked, which is also executed to the end.
This idea improves the efficiency of transactions and allows the same operations to be lined up in parallel only so that atomic operations block and only wait if they conflict.