Giriş
Açıklaması şöyle
ÖrnekA weaker form of select for update is the select for share query. It is an ideal for ensuring referential integrity when creating child records for a parent.
Açıklaması şöyle
Suppose that we want to create a new purchase for a user. First, we would select the user from the database and then insert a new record in the purchases database. Can we safely insert a new purchase into the database? With a regular select statement we can’t. Other processes could delete the user in the moments between selecting the user and inserting the purchase.One way to avoid potential issues is to query for the user with the FOR SHARE locking clause.
Şöyle yaparız
process A: BEGIN;process A: SELECT * FROM users WHERE id = 1 FOR SHARE;process B: DELETE FROM users WHERE id = 1;-- process B blocks and must wait for process A to finishprocess A: INSERT INTO purchases (id, user_id) VALUES (1, 1);process A: COMMIT;-- process B now unblocks and deletes the user
Açıklaması şöyle
Select for share prevented other processes from deleting the user, but does not prevent concurrent processes from selecting users. This is the major difference between select for share and select for update.The select for share prevents updates and deletes of rows, but doesn’t prevent other processes from acquiring a select for share. On the other hand, select for update also blocks updates and deletes, but it also prevents other processes from acquiring a select for update lock.
Hiç yorum yok:
Yorum Gönder