1.可以实现互斥:select...for update 与select...for update之间的互斥
事务A:
$ret = mysql_query("BEGIN",$con);
$sql = "select * from taskstatus where aid=300 for update";
$ret = mysql_query($sql,$con);
sleep(10);
$sql = "update taskstatus set aid = 100 where aid = 300";
$ret = mysql_query($sql,$con);
ysql_query("COMMIT",$con);
事务B:
$ret = mysql_query("BEGIN",$con);
$sql = "select * from taskstatus where aid=400 for update";
$ret = mysql_query($sql,$con);
sleep(10);
$sql = "update taskstatus set aid = 100 where aid = 300";
$ret = mysql_query($sql,$con);
ysql_query("COMMIT",$con);
事务A和事务B在select的时候就已经是互斥的了。即如果事务A先执行,B后执行,则B的select...for update会阻塞,直到A完成COMMIT之后才会被行.
2.可以实现互斥:select...for update与update之间的互斥
事务A:
$ret = mysql_query("BEGIN",$con);
$sql = "select * from taskstatus where aid=300 for update"; //select ... for update
$ret = mysql_query($sql,$con);
sleep(10);
$sql = "update taskstatus set aid = 100 where aid = 300"; // update
$ret = mysql_query($sql,$con);
ysql_query("COMMIT",$con);
事务B:
$ret = mysql_query("BEGIN",$con);
$sql = "select * from taskstatus where aid=300"; //select
$ret = mysql_query($sql,$con);
$sql = "update taskstatus set aid = 100 where aid = 300"; //update
$ret = mysql_query($sql,$con);
ysql_query("COMMIT",$con);
事务A的select...for update可以与事务B的update语句实现互斥,即如果事务A先执行,B后执行,A执行完select...for update,B执行select不会被阻塞,但是B在执行update的时候会阻塞(此时A还没有执行update),在事务A COMMIT之后B的update才会被执行.
3.事务A中的select ... for update 与事务B中的select 之间不会互斥
4.说明:
a.如果表中没有任保记录,则事务之间不会出现互斥(需验证)
b.只有InnoDb类型的表才支持for update事务互斥(需验证)