æ¬çªç°å¢ã§ããããã¡ãã£ã人 Advent Calendarã§ããã®ãã¿ã¼ã³ã®ããããã¯ãªãã£ãã®ã§ãã¼ãã¼ããå©ããã¨ã«ãã¾ããã
çªå¤ç·¨ã®ã¤ããã§ã楽ãã¿ãã ããã
ãã®è¨äºããæ°ããªé害çºçãé²ããã¨ã«ã¤ãªããã°å¹¸ãã§ãã
ä½ããã£ãã®ã
ããæ¥ãã¡ãã£ã¨ãã調æ»ã®ããã«æ¬çªãã¼ã¿ãã¼ã¹ã®ãã¼ã¿ã確èªãããã¨ã«ãªãã¾ããã
ï¼å人æ
å ±ãæ ¼ç´ããã¦ãããããªã·ã¹ãã ã§ã¯ãªãã£ãã®ã§ãå¿
è¦ã§ããã°æ¬çªãã¼ã¿ãã¼ã¹ã¸ã®ã¢ã¯ã»ã¹ã許ããã¦ãã¾ããï¼
ããã¡ã³ããã³ã¹ãããã°ãã®ã¿ã¤ãã³ã°ã§ããã°ããã£ãã®ã§ãããç´è¿ã§ã¯ç¹ã«äºå®ã¯ãªãã¨ã®ãã¨ã§ããããã®ãããã·ã¹ãã ãåãã¦ããç¶æ
ã®ã¾ã¾ä½æ¥ããããã¨ã«ãã¾ããã
ããåç´ãª SELECT
ãå®è¡ããã ãã®ã¤ããã ã£ãã®ã§ãã·ã¹ãã ã«å½±é¿ããªãã¨å¤æããããã§ãã
ãã®éãä¸ãä¸ã³ããããã¹ã£ã¦æ´æ°ç³»ã® SQL ãå®è¡ãã¦ãã¾ã£ããæãã®ã§ã念ã®ãããã©ã³ã¶ã¯ã·ã§ã³ãããã¦ããSQLãå®è¡ãããã¨ã«ãã¾ããã
å
·ä½çã«ã¯ãpsql ï¼PostgreSQL ã®ã¿ã¼ããã«ï¼ã§æ¬çªãã¼ã¿ãã¼ã¹ã«ç¹ãã§ã以ä¸ã® SQL ãå®è¡ãã¾ããã
BEGIN; SELECT * FROM user_setting WHERE xxx = 1;
çµæã¯ããã«å¸°ã£ã¦ãã¾ããã確ã2è¡ç¨åº¦ã ã£ãã¨æãã¾ãã
ç¶ãã¦ããã« SQL ãå®è¡ãããã¨ãã¾ãããããããããã§ååãããã½ã¼ã¹ã³ã¼ãã§ããããªãã¨ããããããã§ããâ¦ãã¨å£°ãããããã¾ããã
ãã¡ãã¯æ¥ãã®ä½æ¥ã§ã¯ãªãã£ãã®ã§ãã¿ã¼ããã«ããã®ã¾ã¾ã«ãã¦ååã®è³ªåã«åçãããã¨ã«ãã¾ããã
ããã¦ç´10åå¾â¦ã
ãã·ã¹ãã ããã¦ã³ãã¦ãã¼ï¼ã
æ¬çªé害ã¨ãªãã¾ããã
ä½ãæªãã£ãã®ã
ããã©ã³ã¶ã¯ã·ã§ã³ããã㦠SELECT
æãæã£ããåãæªããã¨ãããã¨ã«ãªãã¾ããã
ä½ãèµ·ãã¦ããã®ã
ãã°ããã·ã¹ãã ã®åãã確èªããã¨ãããããã¹ã¬ãã㧠user_setting
ãã¼ãã«ãããã¯ãããã¨ãã¦ãããã¨ãåããã¾ãããå
·ä½çã«ã¯ã以ä¸ã® SQL ãçºè¡ããã¦ãã¾ããã
LOCK TABLE user_setting;
ãã® SQL ã«ã¯ãããã¯ã¢ã¼ãã®æå®ãããã¾ããããã®å ´åãPostgreSQL 㯠ACCESS EXCLUSIVE
ããã¯ãæå®ããããã®ã¨ã¿ãªããã¾ãã
â æ示çãã㯠- PostgreSQL 9.4.5ææ¸
ãã® ACCESS EXCLUSIVE
ããã¯ã¯æãå¼·ããã¼ãã«ããã¯ã§ãã
SELECT å®è¡æã«èªåçã«ç²å¾ãããæãå¼±ããã¼ãã«ããã¯ã§ãã ACCESS SHARE
ããã¯ã¨ã競åãã¾ãã
ã¤ã¾ããã·ã¹ãã ã LOCK TABLE
æã«ãã£ã¦ user_setting
ãã¼ãã«ã® ACCESS EXCLUSIVE
ããã¯ãç²å¾ãããã¨ãã¾ããããç§ãå
ã« SELECT
æã«ãã£ã¦ ACCESS SHARE
ããã¯ãç²å¾ãã¦ãããã¨ã§ãããã¯è§£é¤å¾
ã¡ã«å
¥ã£ã¦å¦çãæ¢ã¾ã£ã¦ãã¾ãã¾ããã
ããã«ããã®ãã¨å¥ã®ã¹ã¬ããã user_setting
ãã¼ãã«ã«å¯¾ã SELECT
ãå®è¡ãããã¨ãã¦ãã¾ãããããããuser_setting
ãã¼ãã«ã¯ ACCESS EXCLUSIVE
ããã¯ã®ç²å¾å¾
ã¡ãçºçãã¦ããã®ã§ããã® SELECT
æãæ¢ã¾ã£ã¦ãã¾ã£ã¦ãã¾ããã
çµæãä¸ã¤ã®ã¹ã¬ããã LOCK TABLE
ã§ãå¤æ°ã®ã¹ã¬ããã SELECT
ã§æ¢ã¾ã£ã¦ãã¾ãããã¼ã¿ãã¼ã¹ã¨ã®ã³ãã¯ã·ã§ã³ãã¼ã«ãæ¯æ¸ãã·ã¹ãã ãã¦ã³ã«è³ãã¾ããã
ã©ãããã°ããã£ãã®ã
äºåº¦ã¨æ¨åãèµ·ãããªãããã«ã以ä¸ã®ç¥è¦ãå¾ã¾ããã
SELECT
ããããªãã¨ã¯ãããæ²¹æããªã- ãã©ã³ã¶ã¯ã·ã§ã³ãéå§ãããªããæ¾ç½®ããªã
- ãªãã¹ãæ©ã
COMMIT
ãROLLBACK
ããã
- ãªãã¹ãæ©ã
ã¾ããç§ãã·ã¹ãã ãè¨è¨ããéã«ä»¥ä¸ã®ç¹ã«æ°ãä»ããããã«ãªãã¾ããã
- ããã¯ç²åº¦ã«æ³¨æãã
LOCK TABLE
ã¯æ¥µå使ç¨ããªã- ãããå¾ã
LOCK TABLE
ã使ç¨ãããªããå¯è½ãªéãå¼±ãããã¯ã¢ã¼ãã使ç¨ãã- ãã®éã¯ã
LOCK TABLE
ã¨ã®ç«¶åã«ã¤ãã¦å¨ç¥ãã1
- ãã®éã¯ã
- ãããå¾ã
MySQL ã§ã¯
MySQL ã ã¨ãã¼ãã«ããã¯ã®ããã¯ã¢ã¼ãã«ã¯ READ 㨠WRITE ã®2種é¡ãããã¾ãã
MySQL :: MySQL 5.6 ãªãã¡ã¬ã³ã¹ããã¥ã¢ã« :: 13.3.5 LOCK TABLES ããã³ UNLOCK TABLES æ§æ
ãã®ãã¡ WRITE ããã¯ã¯ SELECT ã¨ç«¶åãã¾ãã
ã¾ããMySQL ã®ãã¼ãã«ããã¯ã¯ã»ãã·ã§ã³åä½ã§ããããã¥ã¡ã³ãã«ããããã«ãROLLBACK ããã ãã§ã¯ããã¯ã解é¤ãããªãã®ã§æ³¨æãå¿
è¦ã§ãã
- ãã©ã³ã¶ã¯ã·ã§ã³ã (ãã¨ãã°ãSTART TRANSACTION ã§) éå§ããã¨ãç¾å¨ã®ãã©ã³ã¶ã¯ã·ã§ã³ã¯ãã¹ã¦æé»çã«ã³ããããããæ¢åã®ãã¼ãã«ããã¯ã解æ¾ããã¾ãã
- ï¼ä¸ç¥ï¼
- ROLLBACK ã¯ããã¼ãã«ããã¯ã解æ¾ãã¾ããã
å¾æ¥è«
ãã®ä»¶ãªã©ã§ã¹ãã¬ã¹ãéãªã£ããã¨ã§èçæ£ããããã¦ãä»åº¦ã¯ç§ããã¦ã³ãã¾ãã2ã人éã®ä½ãæ¬çªé害ãèµ·ãããã§ããâ¦ã
ã¿ãªããããé害çºçå¾ã®ä½èª¿ç®¡çã«ã¯ååã注æãã ããï¼