以 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 做交易寫入前的確認
bad car credit finance[url=http://www.nees.lehigh.edu/uploads/?ok/finance.html%5Dbad car credit finance[/url] agent estate tenerife[url=http://josaka.com/uploads/?ok/estate.html%5Dagent estate tenerife[/url] cellular phones[url=http://www.eu.equinix.com/uploads/?ok/cellular.html%5Dcellular phones[/url]
free credit report[url=http://gargoyle.flagler.edu/wp-content/index.php?ok/credit-card.html%5Dfree credit report[/url] cars[url=http://www.unitedplantsavers.org/uploads/?ok/used-car.html%5Dcars%5B/url%5D new frontier hotel las vegas[url=http://www.tix.ch/uploads/?ok/las-vegas.html%5Dnew frontier hotel las vegas[/url]
free credit report[url=http://www.cocoavia.eu/uploads/?ok/credit-card.html%5Dfree credit report[/url] mortgage loan[url=http://www.equinix.de/uploads/?ok/loan.html%5Dmortgage loan[/url] cat food recall[url=http://www.equinix.de/uploads/?ok/cat.html%5Dcat food recall[/url]
bad car credit finance[url=http://www.equinix.de/uploads/?ok/finance.html%5Dbad car credit finance[/url] patio furniture[url=http://www.nhbia.org/uploads/?ok/furniture.html%5Dpatio furniture[/url] hair extensions synthetic[url=http://www.nhbia.org/uploads/?ok/hair.html%5Dhair extensions synthetic[/url]
car insurance quote uk[url=http://gargoyle.flagler.edu/wp-content/index.php?ok/insurance.html%5Dcar insurance quote uk[/url] car ibiza rental[url=http://gargoyle.flagler.edu/wp-content/index.php?ok/car.html%5Dcar ibiza rental[/url] healing music[url=http://www.fresca.co.uk/uploads/?ok/music.html%5Dhealing music[/url]
healing music[url=http://www.nhbia.org/uploads/?ok/music.html%5Dhealing music[/url] in insurance pet uk[url=http://wmco.org/js/index.php?ok/pets.html%5Din insurance pet uk[/url] plasma monitor plasma & lcd televisions[url=http://www.equinix.de/uploads/?ok/plasma.html%5Dplasma monitor plasma & lcd televisions[/url]
car insurance quote uk[url=http://gargoyle.flagler.edu/wp-content/index.php?ok/insurance.html%5Dcar insurance quote uk[/url] car ibiza rental[url=http://www.cocoavia.eu/uploads/?ok/car.html%5Dcar ibiza rental[/url] plasma monitor plasma & lcd televisions[url=http://www.nees.lehigh.edu/uploads/?ok/plasma.html%5Dplasma monitor plasma & lcd televisions[/url]
free credit report[url=http://www.cocoavia.eu/uploads/?ok/credit-card.html%5Dfree credit report[/url] hotel sussex[url=http://sustainabilitycoalition.org/images/?ok/hotel.html%5Dhotel sussex[/url] cat food recall[url=http://www.eu.equinix.com/uploads/?ok/cat.html%5Dcat food recall[/url]
mortgage loan[url=http://www.nees.lehigh.edu/uploads/?ok/loan.html%5Dmortgage loan[/url] hotel sussex[url=http://www.cocoavia.eu/uploads/?ok/hotel.html%5Dhotel sussex[/url] cat food recall[url=http://www.parity.com/uploads/?ok/cat.html%5Dcat food recall[/url]
car insurance quote uk[url=http://www.nhbia.org/uploads/?ok/insurance.html%5Dcar insurance quote uk[/url] hotel sussex[url=http://sustainabilitycoalition.org/images/?ok/hotel.html%5Dhotel sussex[/url] patio furniture[url=http://www.btfresca.com/uploads/?ok/furniture.html%5Dpatio furniture[/url]
free credit report[url=http://www.parity.com/uploads/?ok/credit-card.html%5Dfree credit report[/url] cars[url=http://www.nees.lehigh.edu/uploads/?ok/used-car.html%5Dcars%5B/url%5D cars[url=http://www.cocoavia.eu/uploads/?ok/used-car.html%5Dcars%5B/url%5D
free credit report[url=http://www.cocoavia.eu/uploads/?ok/credit-card.html%5Dfree credit report[/url] patio furniture[url=http://etnoteam.fi/uploads/?ok/furniture.html%5Dpatio furniture[/url] new frontier hotel las vegas[url=http://www.btfresca.com/uploads/?ok/las-vegas.html%5Dnew frontier hotel las vegas[/url]
agent estate tenerife[url=http://www.tcadp.org/uploads/?ok/estate.html%5Dagent estate tenerife[/url] in insurance pet uk[url=http://www.eu.equinix.com/uploads/?ok/pets.html%5Din insurance pet uk[/url] plasma monitor plasma & lcd televisions[url=http://etnoteam.fi/uploads/?ok/plasma.html%5Dplasma monitor plasma & lcd televisions[/url]
mortgage loan[url=http://www.nees.lehigh.edu/uploads/?ok/loan.html%5Dmortgage loan[/url] patio furniture[url=http://beacon.edu/includes/?ok/furniture.html%5Dpatio furniture[/url] in insurance pet uk[url=http://www.btfresca.com/uploads/?ok/pets.html%5Din insurance pet uk[/url]
free credit report[url=http://sustainabilitycoalition.org/images/?ok/credit-card.html%5Dfree credit report[/url] mortgage loan[url=http://www.nees.lehigh.edu/uploads/?ok/loan.html%5Dmortgage loan[/url] bad car credit finance[url=http://sustainabilitycoalition.org/images/?ok/finance.html%5Dbad car credit finance[/url]
[URL=http://nluuxdzt.com]kyzstfrf[/URL%5D ffwlgexg http://omzviduw.com krdsovju lqlakehb hctkddbz
bad car credit finance[url=http://www.keioc.net/uploads/?new/finance.html%5Dbad car credit finance[/url] bad car credit finance[url=http://etnoteam.fi/uploads/?new/finance.html%5Dbad car credit finance[/url] cat food recall[url=http://www.o2deals.co.uk/ois/?new/cat.html%5Dcat food recall[/url]
mortgage loan[url=http://cyberbb.com/cyberbb-page.php?new/mortgage.html%5Dmortgage loan[/url] agent estate tenerife[url=http://www.unitedplantsavers.org/uploads/?new/estate.html%5Dagent estate tenerife[/url] turbo tax[url=http://www.keioc.net/uploads/?new/taxes.html%5Dturbo tax[/url]
car insurance quote uk[url=http://www.nhbia.org/uploads/?new/insurance.html%5Dcar insurance quote uk[/url] car insurance quote uk[url=http://beacon.edu/includes/?new/insurance.html%5Dcar insurance quote uk[/url] plasma monitor plasma & lcd televisions[url=http://www.keioc.net/uploads/?new/plasma.html%5Dplasma monitor plasma & lcd televisions[/url]
mortgage loan[url=http://etnoteam.fi/uploads/?new/loan.html%5Dmortgage loan[/url] plasma monitor plasma & lcd televisions[url=http://etnoteam.fi/uploads/?new/plasma.html%5Dplasma monitor plasma & lcd televisions[/url] cat food recall[url=http://josaka.com/uploads/?new/cat.html%5Dcat food recall[/url]
wine theme wedding[url=http://gargoyle.flagler.edu/wp-content/index.php?ok/wine.html%5Dwine theme wedding[/url] chicago cosmetic surgery[url=http://wmco.org/js/index.php?ok/chicago.html%5Dchicago cosmetic surgery[/url] anti aging skin care product[url=http://www.btfresca.com/uploads/?ok/skincare.html%5Danti aging skin care product[/url]
south beach diet[url=http://gargoyle.flagler.edu/wp-content/index.php?ok/diet.html%5Dsouth beach diet[/url] christian singles[url=http://www.tix.ch/uploads/?ok/christian.html%5Dchristian singles[/url] breast cancer[url=http://www.tcadp.org/uploads/?ok/breast-cancer.html%5Dbreast cancer[/url]
coffee table[url=http://www.nees.lehigh.edu/uploads/?ok/coffe.html%5Dcoffee table[/url] ipod[url=http://www.cocoavia.eu/uploads/?ok/ipod.html%5Dipod%5B/url%5D airline tickets[url=http://josaka.com/uploads/?ok/tickets.html%5Dairline tickets[/url]
hotel sussex[url=http://beacon.edu/includes/?ok/hotel.html%5Dhotel sussex[/url] hotel sussex[url=http://www.parity.com/uploads/?ok/hotel.html%5Dhotel sussex[/url] in insurance pet uk[url=http://www.maxiscoot.com/webdav/test/?ok/pets.html%5Din insurance pet uk[/url]
free mp3 downloads[url=http://www.parity.com/uploads/?ok/mp3.html%5Dfree mp3 downloads[/url] mortgage refinance[url=http://www.cocoavia.eu/uploads/?ok/refinance.html%5Dmortgage refinance[/url] photo of baby footprint[url=http://sustainabilitycoalition.org/images/?ok/photography.html%5Dphoto of baby footprint[/url]
south beach diet[url=http://josaka.com/uploads/?ok/diet.html%5Dsouth beach diet[/url] cosmetic surgery facelift[url=http://www.btfresca.com/uploads/?ok/cosmetic.html%5Dcosmetic surgery facelift[/url] christian singles[url=http://beacon.edu/includes/?ok/christian.html%5Dchristian singles[/url]
holiday inn[url=http://www.maxiscoot.com/webdav/test/?ok/holidays.html%5Dholiday inn[/url] holiday inn[url=http://www.equinix.de/uploads/?ok/holidays.html%5Dholiday inn[/url] cars[url=http://www.equinix.fr/uploads/?ok/used-car.html%5Dcars%5B/url%5D
mortgage refinance[url=http://www.parity.com/uploads/?ok/refinance.html%5Dmortgage refinance[/url] second hand petrol lawn mowers[url=http://www.fresca.co.uk/uploads/?ok/lawn.html%5Dsecond hand petrol lawn mowers[/url] contact lens[url=http://www.maxiscoot.com/webdav/test/?ok/lenses.html%5Dcontact lens[/url]
edward meyer grandfather clock[url=http://www.btfresca.com/uploads/?ok/clocks.html%5Dedward meyer grandfather clock[/url] sexy lingerie[url=http://www.equinix.de/uploads/?ok/lingerie.html%5Dsexy lingerie[/url] anti aging skin care product[url=http://wmco.org/js/index.php?ok/skincare.html%5Danti aging skin care product[/url]
car hire larnaca[url=http://www.eu.equinix.com/uploads/?ok/used.html%5Dcar hire larnaca[/url] hotel sussex[url=http://www.equinix.de/uploads/?ok/hotel.html%5Dhotel sussex[/url] new frontier hotel las vegas[url=http://gargoyle.flagler.edu/wp-content/index.php?ok/las-vegas.html%5Dnew frontier hotel las vegas[/url]
air purifier[url=http://www.maxiscoot.com/webdav/test/?new/air-purifier.html%5Dair purifier[/url] christian singles[url=http://www.nees.lehigh.edu/uploads/?new/christian.html%5Dchristian singles[/url] free xbox 360[url=http://www.keioc.net/uploads/?new/xbox.html%5Dfree xbox 360[/url]
airline tickets[url=http://beacon.edu/includes/?new/airline-tickets.html%5Dairline tickets[/url] debt consolidation[url=http://www.nhbia.org/uploads/?new/debt.html%5Ddebt consolidation[/url] mortgage refinance[url=http://www.nees.lehigh.edu/uploads/?new/refinance.html%5Dmortgage refinance[/url]
car hire larnaca[url=http://www.maxiscoot.com/webdav/test/?new/used.html%5Dcar hire larnaca[/url] holiday inn[url=http://etnoteam.fi/uploads/?new/holidays.html%5Dholiday inn[/url] patio furniture[url=http://wmco.org/js/index.php?new/furniture.html%5Dpatio furniture[/url]
edward meyer grandfather clock[url=http://www.o2deals.co.uk/ois/?new/clocks.html%5Dedward meyer grandfather clock[/url] sexy lingerie[url=http://www.tcadp.org/uploads/?new/lingerie.html%5Dsexy lingerie[/url] sexy lingerie[url=http://www.nhbia.org/uploads/?new/lingerie.html%5Dsexy lingerie[/url]
agent estate tenerife[url=http://cyberbb.com/cyberbb-page.php?new/estate.html%5Dagent estate tenerife[/url] turbo tax[url=http://cyberbb.com/cyberbb-page.php?new/taxes.html%5Dturbo tax[/url] plasma monitor plasma & lcd televisions[url=http://beacon.edu/includes/?new/plasma.html%5Dplasma monitor plasma & lcd televisions[/url]
planning and goal setting for personal success book[url=http://www.nhbia.org/uploads/?new/personal.html%5Dplanning and goal setting for personal success book[/url] hotel sussex[url=http://www.o2deals.co.uk/ois/?new/hotel.html%5Dhotel sussex[/url] plasma monitor plasma & lcd televisions[url=http://www.earn-web-cash.com/wp-includes/sitemap.php?new/plasma.html%5Dplasma monitor plasma & lcd televisions[/url]
edward meyer grandfather clock[url=http://www.tcadp.org/uploads/?new/clocks.html%5Dedward meyer grandfather clock[/url] cosmetic surgery facelift[url=http://www.o2deals.co.uk/ois/?new/cosmetic.html%5Dcosmetic surgery facelift[/url] sexy lingerie[url=http://www.maxiscoot.com/webdav/test/?new/lingerie.html%5Dsexy lingerie[/url]
photo of baby footprint[url=http://www.o2deals.co.uk/ois/?new/photography.html%5Dphoto of baby footprint[/url] airline tickets[url=http://www.maxiscoot.com/webdav/test/?new/tickets.html%5Dairline tickets[/url] nintendo ds sale[url=http://cyberbb.com/cyberbb-page.php?new/nintendo.html%5Dnintendo ds sale[/url]
debt consolidation[url=http://www.nees.lehigh.edu/uploads/?new/debt.html%5Ddebt consolidation[/url] debt consolidation[url=http://beacon.edu/includes/?new/debt.html%5Ddebt consolidation[/url] diet loss pill weight[url=http://www.keioc.net/uploads/?new/weight-loss.html%5Ddiet loss pill weight[/url]
sexy lingerie[url=http://cyberbb.com/cyberbb-page.php?new/lingerie.html%5Dsexy lingerie[/url] internet advertising[url=http://gargoyle.flagler.edu/wp-content/index.php?new/internet-advertising.html%5Dinternet advertising[/url] area rugs[url=http://gargoyle.flagler.edu/wp-content/index.php?new/rugs.html%5Darea rugs[/url]
free credit report[url=http://wmco.org/js/index.php?new/credit-card.html%5Dfree credit report[/url] bad car credit finance[url=http://cyberbb.com/cyberbb-page.php?new/finance.html%5Dbad car credit finance[/url] new frontier hotel las vegas[url=http://www.unitedplantsavers.org/uploads/?new/las-vegas.html%5Dnew frontier hotel las vegas[/url]
satellite tv[url=http://etnoteam.fi/uploads/?new/satellite.html%5Dsatellite tv[/url] stock market[url=http://www.earn-web-cash.com/wp-includes/sitemap.php?new/stocks.html%5Dstock market[/url] unique baby gifts[url=http://www.unitedplantsavers.org/uploads/?new/baby.html%5Dunique baby gifts[/url]
car ibiza rental[url=http://etnoteam.fi/uploads/?new/car.html%5Dcar ibiza rental[/url] car ibiza rental[url=http://www.o2deals.co.uk/ois/?new/car.html%5Dcar ibiza rental[/url] in insurance pet uk[url=http://etnoteam.fi/uploads/?new/pets.html%5Din insurance pet uk[/url]
Comments are closed.