mysql 排他,mysql 共享锁 排他锁 防插入锁
試驗1
事務1:
#!/usr/bin/python
import time
import MySQLdb;
conn = MySQLdb.connect(host="localhost",port=3306,user="root",passwd="asdf",db="test",unix_socket="/data/mysql_3306/mysql.sock")
cursor = conn.cursor()
cursor.execute("select * from test")
while str!="1":
str = raw_input()
cursor.execute("update test id=id-1")
while str!="exit":
str = raw_input()
cursor.close()
conn.close()
在mysql命令行中輸入以下:
mysql> select * from test;
當事務1中等待輸入1時,顯示出select匹配的行
當事務1中輸入1時,顯示結果如下:
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
結論:當select時會檢測當前是否有事務會修改(比影響要確切一些)當前的記錄時,才會被阻塞。
試驗2
在事務1:
#!/usr/bin/python
import time
import MySQLdb;
conn = MySQLdb.connect(host="localhost",port=3306,user="root",passwd="asdf",db="test",unix_socket="/data/mysql_3306/mysql.sock")
cursor = conn.cursor()
cursor.execute("select * from test lock in share mode")
while str!="exit":
str = raw_input()
cursor.close()
conn.close()
在mysql命令行中輸入以下:
mysql> update test set id=id-1;
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
結論:當事務中包含select ...lock in share mode的時候,相關記錄將會被鎖住,不允許進行修改。
試驗3
執行下面的python腳本兩遍,并且同時輸入1,2之后
#!/usr/bin/python
import time
import MySQLdb;
conn = MySQLdb.connect(host="localhost",port=3306,user="root",passwd="asdf",db="test",unix_socket="/data/mysql_3306/mysql.sock")
cursor = conn.cursor()
cursor.execute("select * from test lock in share mode")
str = ""
while str!="1":
str = raw_input()
while str!="2":
str = raw_input()
cursor.execute("update test set id=id-1")
while str!="exit":
str = raw_input()
cursor.close()
conn.close()
[root@TJSJHL196-139 tmp]# python test_transaction.py
1
2
Traceback (most recent call la
總結
以上是生活随笔為你收集整理的mysql 排他,mysql 共享锁 排他锁 防插入锁的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php 导入表格数据,PHPExcel
- 下一篇: java poll()是什么方法,JAV