12 Haziran 2023 Pazartesi

Select For Share - Read Lock

Giriş
Açıklaması şöyle
Shared lock, also known as a read lock, allows multiple transactions to read the same record concurrently.

Transactions that acquire a shared lock can only read but not modify the data. It’s like a “no harm done” situation, where no transaction can modify the data while others are reading it.

If other transactions attempt to modify the record, they will be blocked until all shared locks are released.
Örnek
Şöyle yaparız
/* session 1 acquires a shared lock */
mysql> begin;

mysql> select * from user_tab where user_id=101 for share;
+---------+---------+------+-------------------+
| id      | user_id | age  | email             |
+---------+---------+------+-------------------+
| 1000076 |     101 |   10 | peter@hotmail.com |
+---------+---------+------+-------------------+


/* session 2 is blocked */
mysql> begin;

mysql> update user_tab set age=11 where user_id=101;
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction


Hiç yorum yok:

Yorum Gönder

Soft Delete

Giriş Açıklaması  şöyle When using the soft delete mechanism on the database, you might run into a situation where a record with a unique co...