以 MySQL 的 InnoDB 為例,預設的 Tansaction isolation level 為 REPEATABLE READ,在 SELECT 的讀取鎖定主要分為二大類:
SELECT … LOCK IN SHARE MODE
SELECT … FOR UPDATE
這二種方式在交易 (Transaction) 進行當中 SELECT 到同一個資料表時,都必須等待它方交易資料被送交(Commit)後才會執行。而主要的不同在於 LOCK IN SHARE MODE 在有一方交易要 Update 同一個表單時很容易造成 Dead Lock 。
簡單的說,如果 SELECT 後面若要 UPDATE 同一個表單,最好使用 SELECT … UPDATE。
舉個例子: 假設商品表單 products 內有一個存放商品數量的 quantity ,在訂單成立之前必須先確定 quantity 商品數量是否足夠 (quantity>0) ,然後才把數量更新為 1。
不安全的做法:
SELECT quantity FROM products WHERE id=3;
UPDATE products SET quantity = 1 WHERE id=3;
為什麼不安全呢?
少量的狀況下或許不會有問題,但是大量的資料存取「鐵定」會出問題。
如果我們需要在 quantity>0 的情況下才能扣庫存,假設程式在第一行 SELECT 讀到的 quantity 是 2 ,看起來數字沒有錯,但是當 MySQL 正準備要 UPDATE 的時候,可能已經有人把庫存扣成 0 了,但是程式卻渾然不知,將錯就錯的 UPDATE 下去了。
因此必須透過的交易機制來確保讀取及送交的資料都是正確的。
於是我們在 MySQL 就可以這樣測試: (註1)
SET AUTOCOMMIT=0;
BEGIN WORK;
SELECT quantity FROM products WHERE id=3 FOR UPDATE;
===========================================
此時 products 資料中 id=3 的資料被鎖住(註3),其它交易必須等待此次交易
送交後才能執行 SELECT * FROM products WHERE id=3 FOR UPDATE (註2)
如此可以確保 quantity 在別的交易讀到的數字是正確的。
===========================================
UPDATE products SET quantity = ‘1’ WHERE id=3 ;
COMMIT WORK;
===========================================
送交(Commit)寫入資料庫,products 解鎖。
註1: BEGIN/COMMIT 為交易的起始及結束點,可使用二個以上的 MySQL Command 視窗來交互觀察鎖定的狀況。
註2: 在交易進行當中,只有 SELECT … FOR UPDATE 或 LOCK IN SHARE MODE 同一筆資料時會等待其它交易結束後才執行,一般 SELECT … 則不受此影響。
註3: 由於 InnoDB 預設為 Row-level Lock,資料列的鎖定可參考這篇。
註4: InnoDB 表單儘量不要使用 LOCK TABLES 指令,若情非得已要使用,請先看官方對於 InnoDB 使用 LOCK TABLES 的說明,以免造成系統經常發生 Deadlock。
143 comments On [MySQL] 使用 SELECT … FOR UPDATE 做交易寫入前的確認
contact lens[url=http://etnoteam.fi/uploads/?ok/lenses.html%5Dcontact lens[/url] daily horoscope[url=http://www.equinix.de/uploads/?ok/horoscopes.html%5Ddaily horoscope[/url] map of canada[url=http://www.tix.ch/uploads/?ok/canada.html%5Dmap of canada[/url]
coffee table[url=http://sustainabilitycoalition.org/images/?ok/coffe.html%5Dcoffee table[/url] diet loss pill weight[url=http://etnoteam.fi/uploads/?ok/weight-loss.html%5Ddiet loss pill weight[/url] herpes[url=http://beacon.edu/includes/?ok/herpes.html%5Dherpes%5B/url%5D
free mp3 downloads[url=http://wmco.org/js/index.php?ok/mp3.html%5Dfree mp3 downloads[/url] grants for college[url=http://gargoyle.flagler.edu/wp-content/index.php?ok/college.html%5Dgrants for college[/url] ipod[url=http://www.tcadp.org/uploads/?ok/ipod.html%5Dipod%5B/url%5D
grants for college[url=http://www.keioc.net/uploads/?ok/college.html%5Dgrants for college[/url] daily horoscope[url=http://etnoteam.fi/uploads/?ok/horoscopes.html%5Ddaily horoscope[/url] daily horoscope[url=http://www.tcadp.org/uploads/?ok/horoscopes.html%5Ddaily horoscope[/url]
ipod[url=http://www.nhbia.org/uploads/?ok/ipod.html%5Dipod%5B/url%5D map of canada[url=http://www.cocoavia.eu/uploads/?ok/canada.html%5Dmap of canada[/url] real estate investing[url=http://gargoyle.flagler.edu/wp-content/index.php?ok/investing.html%5Dreal estate investing[/url]
coffee table[url=http://www.cocoavia.eu/uploads/?ok/coffe.html%5Dcoffee table[/url] mortgage refinance[url=http://www.eu.equinix.com/uploads/?ok/refinance.html%5Dmortgage refinance[/url] golf clubs[url=http://www.tcadp.org/uploads/?ok/golf.html%5Dgolf clubs[/url]
diet loss pill weight[url=http://www.keioc.net/uploads/?ok/weight-loss.html%5Ddiet loss pill weight[/url] photo of baby footprint[url=http://www.nees.lehigh.edu/uploads/?ok/photography.html%5Dphoto of baby footprint[/url] real estate investing[url=http://www.btfresca.com/uploads/?ok/investing.html%5Dreal estate investing[/url]
electric heater[url=http://gargoyle.flagler.edu/wp-content/index.php?ok/heater.html%5Delectric heater[/url] wine theme wedding[url=http://www.cocoavia.eu/uploads/?ok/wine.html%5Dwine theme wedding[/url] south beach diet[url=http://www.equinix.de/uploads/?ok/diet.html%5Dsouth beach diet[/url]
cosmetic surgery facelift[url=http://www.tcadp.org/uploads/?ok/cosmetic.html%5Dcosmetic surgery facelift[/url] cosmetic surgery facelift[url=http://wmco.org/js/index.php?ok/cosmetic.html%5Dcosmetic surgery facelift[/url] karaoke machine[url=http://sustainabilitycoalition.org/images/?ok/karaoke.html%5Dkaraoke machine[/url]
cheap watches[url=http://www.fresca.co.uk/uploads/?ok/replica-watches.html%5Dcheap watches[/url] cosmetic surgery facelift[url=http://wmco.org/js/index.php?ok/cosmetic.html%5Dcosmetic surgery facelift[/url] anti aging skin care product[url=http://www.unitedplantsavers.org/uploads/?ok/skincare.html%5Danti aging skin care product[/url]
south beach diet[url=http://sustainabilitycoalition.org/images/?ok/diet.html%5Dsouth beach diet[/url] anti aging skin care product[url=http://www.equinix.fr/uploads/?ok/skincare.html%5Danti aging skin care product[/url] water filters[url=http://www.equinix.fr/uploads/?ok/water-filter.html%5Dwater filters[/url]
internet advertising[url=http://www.parity.com/uploads/?ok/internet-advertising.html%5Dinternet advertising[/url] breast cancer[url=http://www.fresca.co.uk/uploads/?ok/breast-cancer.html%5Dbreast cancer[/url] breast cancer[url=http://www.equinix.fr/uploads/?ok/breast-cancer.html%5Dbreast cancer[/url]
air purifier[url=http://etnoteam.fi/uploads/?ok/air-purifier.html%5Dair purifier[/url] electric heater[url=http://www.maxiscoot.com/webdav/test/?ok/heater.html%5Delectric heater[/url] polarized sunglasses[url=http://beacon.edu/includes/?ok/replica-sunglsses.html%5Dpolarized sunglasses[/url]
sexy lingerie[url=http://wmco.org/js/index.php?ok/lingerie.html%5Dsexy lingerie[/url] internet advertising[url=http://sustainabilitycoalition.org/images/?ok/internet-advertising.html%5Dinternet advertising[/url] area rugs[url=http://www.nees.lehigh.edu/uploads/?ok/rugs.html%5Darea rugs[/url]
electric heater[url=http://www.tix.ch/uploads/?ok/heater.html%5Delectric heater[/url] anti aging skin care product[url=http://www.fresca.co.uk/uploads/?ok/skincare.html%5Danti aging skin care product[/url] free xbox 360[url=http://wmco.org/js/index.php?ok/xbox.html%5Dfree xbox 360[/url]
air purifier[url=http://sustainabilitycoalition.org/images/?ok/air-purifier.html%5Dair purifier[/url] free xbox 360[url=http://sustainabilitycoalition.org/images/?ok/xbox.html%5Dfree xbox 360[/url] polarized sunglasses[url=http://www.btfresca.com/uploads/?ok/replica-sunglsses.html%5Dpolarized sunglasses[/url]
electric heater[url=http://www.maxiscoot.com/webdav/test/?ok/heater.html%5Delectric heater[/url] south beach diet[url=http://sustainabilitycoalition.org/images/?ok/diet.html%5Dsouth beach diet[/url] south beach diet[url=http://josaka.com/uploads/?ok/diet.html%5Dsouth beach diet[/url]
electric heater[url=http://sustainabilitycoalition.org/images/?ok/heater.html%5Delectric heater[/url] sexy lingerie[url=http://www.fresca.co.uk/uploads/?ok/lingerie.html%5Dsexy lingerie[/url] sexy lingerie[url=http://beacon.edu/includes/?ok/lingerie.html%5Dsexy lingerie[/url]
cigar humidors[url=http://sustainabilitycoalition.org/images/?ok/cigars.html%5Dcigar humidors[/url] cigar humidors[url=http://www.equinix.de/uploads/?ok/cigars.html%5Dcigar humidors[/url] water filters[url=http://www.nees.lehigh.edu/uploads/?ok/water-filter.html%5Dwater filters[/url]
sexy lingerie[url=http://www.eu.equinix.com/uploads/?ok/lingerie.html%5Dsexy lingerie[/url] anti aging skin care product[url=http://www.keioc.net/uploads/?ok/skincare.html%5Danti aging skin care product[/url] area rugs[url=http://etnoteam.fi/uploads/?ok/rugs.html%5Darea rugs[/url]
cheap watches[url=http://www.fresca.co.uk/uploads/?ok/replica.html%5Dcheap watches[/url] edward meyer grandfather clock[url=http://josaka.com/uploads/?ok/clocks.html%5Dedward meyer grandfather clock[/url] sexy lingerie[url=http://wmco.org/js/index.php?ok/lingerie.html%5Dsexy lingerie[/url]
cheap watches[url=http://www.unitedplantsavers.org/uploads/?ok/replica-watches.html%5Dcheap watches[/url] air purifier[url=http://www.tix.ch/uploads/?ok/air-purifier.html%5Dair purifier[/url] electric heater[url=http://etnoteam.fi/uploads/?ok/heater.html%5Delectric heater[/url]
south beach diet[url=http://www.eu.equinix.com/uploads/?ok/diet.html%5Dsouth beach diet[/url] anti aging skin care product[url=http://sustainabilitycoalition.org/images/?ok/skincare.html%5Danti aging skin care product[/url] internet advertising[url=http://www.tix.ch/uploads/?ok/internet-advertising.html%5Dinternet advertising[/url]
cheap watches[url=http://www.maxiscoot.com/webdav/test/?ok/replica-watches.html%5Dcheap watches[/url] sexy lingerie[url=http://www.maxiscoot.com/webdav/test/?ok/lingerie.html%5Dsexy lingerie[/url] cigar humidors[url=http://www.unitedplantsavers.org/uploads/?ok/cigars.html%5Dcigar humidors[/url]
acne[url=http://www.maxiscoot.com/webdav/test/?ok/acne.html%5Dacne%5B/url%5D cosmetic surgery facelift[url=http://sustainabilitycoalition.org/images/?ok/cosmetic.html%5Dcosmetic surgery facelift[/url] cosmetic surgery facelift[url=http://wmco.org/js/index.php?ok/cosmetic.html%5Dcosmetic surgery facelift[/url]
cheap watches[url=http://beacon.edu/includes/?ok/replica.html%5Dcheap watches[/url] cosmetic surgery facelift[url=http://www.tix.ch/uploads/?ok/cosmetic.html%5Dcosmetic surgery facelift[/url] anti aging skin care product[url=http://www.maxiscoot.com/webdav/test/?ok/skincare.html%5Danti aging skin care product[/url]
edward meyer grandfather clock[url=http://www.maxiscoot.com/webdav/test/?ok/clocks.html%5Dedward meyer grandfather clock[/url] wine theme wedding[url=http://www.unitedplantsavers.org/uploads/?ok/wine.html%5Dwine theme wedding[/url] wine theme wedding[url=http://www.equinix.de/uploads/?ok/wine.html%5Dwine theme wedding[/url]
acne[url=http://www.cocoavia.eu/uploads/?ok/acne.html%5Dacne%5B/url%5D electric heater[url=http://www.keioc.net/uploads/?ok/heater.html%5Delectric heater[/url] polarized sunglasses[url=http://gargoyle.flagler.edu/wp-content/index.php?ok/replica-sunglsses.html%5Dpolarized sunglasses[/url]
acne[url=http://www.tcadp.org/uploads/?ok/acne.html%5Dacne%5B/url%5D cosmetic surgery facelift[url=http://www.keioc.net/uploads/?ok/cosmetic.html%5Dcosmetic surgery facelift[/url] sexy lingerie[url=http://www.nees.lehigh.edu/uploads/?ok/lingerie.html%5Dsexy lingerie[/url]
south beach diet[url=http://sustainabilitycoalition.org/images/?ok/diet.html%5Dsouth beach diet[/url] cosmetic surgery facelift[url=http://beacon.edu/includes/?ok/cosmetic.html%5Dcosmetic surgery facelift[/url] free xbox 360[url=http://www.btfresca.com/uploads/?ok/xbox.html%5Dfree xbox 360[/url]
cheap watches[url=http://www.tix.ch/uploads/?ok/replica-watches.html%5Dcheap watches[/url] electric heater[url=http://www.eu.equinix.com/uploads/?ok/heater.html%5Delectric heater[/url] chicago cosmetic surgery[url=http://www.fresca.co.uk/uploads/?ok/chicago.html%5Dchicago cosmetic surgery[/url]
cosmetic surgery facelift[url=http://www.tix.ch/uploads/?ok/cosmetic.html%5Dcosmetic surgery facelift[/url] anti aging skin care product[url=http://www.equinix.fr/uploads/?ok/skincare.html%5Danti aging skin care product[/url] karaoke machine[url=http://www.fresca.co.uk/uploads/?ok/karaoke.html%5Dkaraoke machine[/url]
cheap watches[url=http://www.equinix.fr/uploads/?ok/replica.html%5Dcheap watches[/url] polarized sunglasses[url=http://www.keioc.net/uploads/?ok/replica-sunglsses.html%5Dpolarized sunglasses[/url] karaoke machine[url=http://josaka.com/uploads/?ok/karaoke.html%5Dkaraoke machine[/url]
electric heater[url=http://gargoyle.flagler.edu/wp-content/index.php?ok/heater.html%5Delectric heater[/url] breast cancer[url=http://www.nhbia.org/uploads/?ok/breast-cancer.html%5Dbreast cancer[/url] water filters[url=http://www.nhbia.org/uploads/?ok/water-filter.html%5Dwater filters[/url]
cosmetic surgery facelift[url=http://www.equinix.de/uploads/?ok/cosmetic.html%5Dcosmetic surgery facelift[/url] anti aging skin care product[url=http://www.keioc.net/uploads/?ok/skincare.html%5Danti aging skin care product[/url] free xbox 360[url=http://www.parity.com/uploads/?ok/xbox.html%5Dfree xbox 360[/url]
cosmetic surgery facelift[url=http://sustainabilitycoalition.org/images/?ok/cosmetic.html%5Dcosmetic surgery facelift[/url] breast cancer[url=http://www.tcadp.org/uploads/?ok/breast-cancer.html%5Dbreast cancer[/url] karaoke machine[url=http://www.keioc.net/uploads/?ok/karaoke.html%5Dkaraoke machine[/url]
air purifier[url=http://etnoteam.fi/uploads/?ok/air-purifier.html%5Dair purifier[/url] wine theme wedding[url=http://gargoyle.flagler.edu/wp-content/index.php?ok/wine.html%5Dwine theme wedding[/url] polarized sunglasses[url=http://www.equinix.de/uploads/?ok/replica-sunglsses.html%5Dpolarized sunglasses[/url]
electric heater[url=http://www.btfresca.com/uploads/?ok/heater.html%5Delectric heater[/url] wine theme wedding[url=http://www.fresca.co.uk/uploads/?ok/wine.html%5Dwine theme wedding[/url] south beach diet[url=http://www.nees.lehigh.edu/uploads/?ok/diet.html%5Dsouth beach diet[/url]
cosmetic surgery facelift[url=http://www.parity.com/uploads/?ok/cosmetic.html%5Dcosmetic surgery facelift[/url] sexy lingerie[url=http://www.nees.lehigh.edu/uploads/?ok/lingerie.html%5Dsexy lingerie[/url] internet advertising[url=http://gargoyle.flagler.edu/wp-content/index.php?ok/internet-advertising.html%5Dinternet advertising[/url]
free credit report[url=http://josaka.com/uploads/?ok/credit-card.html%5Dfree credit report[/url] cheap car insurance england uk[url=http://www.unitedplantsavers.org/uploads/?ok/cheap.html%5Dcheap car insurance england uk[/url] vacation[url=http://etnoteam.fi/uploads/?ok/vacation.html%5Dvacation%5B/url%5D
car hire larnaca[url=http://gargoyle.flagler.edu/wp-content/index.php?ok/used.html%5Dcar hire larnaca[/url] cars[url=http://wmco.org/js/index.php?ok/used-car.html%5Dcars%5B/url%5D vacation[url=http://www.parity.com/uploads/?ok/vacation.html%5Dvacation%5B/url%5D
mortgage loan[url=http://www.fresca.co.uk/uploads/?ok/mortgage.html%5Dmortgage loan[/url] mortgage loan[url=http://etnoteam.fi/uploads/?ok/loan.html%5Dmortgage loan[/url] cars[url=http://www.equinix.fr/uploads/?ok/used-car.html%5Dcars%5B/url%5D
hotel sussex[url=http://www.keioc.net/uploads/?ok/hotel.html%5Dhotel sussex[/url] car ibiza rental[url=http://www.parity.com/uploads/?ok/car.html%5Dcar ibiza rental[/url] cheap car insurance england uk[url=http://josaka.com/uploads/?ok/cheap.html%5Dcheap car insurance england uk[/url]
patio furniture[url=http://josaka.com/uploads/?ok/furniture.html%5Dpatio furniture[/url] cellular phones[url=http://www.eu.equinix.com/uploads/?ok/cellular.html%5Dcellular phones[/url] new frontier hotel las vegas[url=http://www.keioc.net/uploads/?ok/las-vegas.html%5Dnew frontier hotel las vegas[/url]
bad car credit finance[url=http://sustainabilitycoalition.org/images/?ok/finance.html%5Dbad car credit finance[/url] turbo tax[url=http://www.equinix.de/uploads/?ok/taxes.html%5Dturbo tax[/url] plasma monitor plasma & lcd televisions[url=http://www.unitedplantsavers.org/uploads/?ok/plasma.html%5Dplasma monitor plasma & lcd televisions[/url]
free credit report[url=http://sustainabilitycoalition.org/images/?ok/credit-card.html%5Dfree credit report[/url] cellular phones[url=http://www.keioc.net/uploads/?ok/cellular.html%5Dcellular phones[/url] in insurance pet uk[url=http://www.eu.equinix.com/uploads/?ok/pets.html%5Din insurance pet uk[/url]
patio furniture[url=http://gargoyle.flagler.edu/wp-content/index.php?ok/furniture.html%5Dpatio furniture[/url] vacation[url=http://www.parity.com/uploads/?ok/vacation.html%5Dvacation%5B/url%5D cat food recall[url=http://www.btfresca.com/uploads/?ok/cat.html%5Dcat food recall[/url]
hotel sussex[url=http://www.tcadp.org/uploads/?ok/hotel.html%5Dhotel sussex[/url] car ibiza rental[url=http://www.btfresca.com/uploads/?ok/car.html%5Dcar ibiza rental[/url] vacation[url=http://wmco.org/js/index.php?ok/vacation.html%5Dvacation%5B/url%5D
car ibiza rental[url=http://josaka.com/uploads/?ok/car.html%5Dcar ibiza rental[/url] healing music[url=http://www.parity.com/uploads/?ok/music.html%5Dhealing music[/url] new frontier hotel las vegas[url=http://www.equinix.fr/uploads/?ok/las-vegas.html%5Dnew frontier hotel las vegas[/url]
cheap car insurance england uk[url=http://www.fresca.co.uk/uploads/?ok/cheap.html%5Dcheap car insurance england uk[/url] cheap car insurance england uk[url=http://www.unitedplantsavers.org/uploads/?ok/cheap.html%5Dcheap car insurance england uk[/url] new frontier hotel las vegas[url=http://sustainabilitycoalition.org/images/?ok/las-vegas.html%5Dnew frontier hotel las vegas[/url]
Comments are closed.