This article mainly introduces how PHP inserts mysql data to implement multi-host mutual lock, including the complete implementation method of locking and releasing locks, which is very practical, for more information about how PHP inserts mysql data to implement multi-host lock, see the following example. The specific implementation method is as follows:
You can add a general lock before executing the process. The shell yoke function is as follows. if it succeeds, 0 is returned; otherwise, a non-0 value is returned:
The code is as follows:
Function get_lock ()
{
Local dataId = "$ {1 }"
Local dataDate = "$ {2 }"
Local SQL = "insert into t_trans_lock
(DataId, dataDate) values ('$ {dataId}', '$ {dataDate }');"
Echo $ {SQL} | $ {DB_PUBLIC}
Return $?
}
Release the lock when execution fails or ends.
The code is as follows:
Function free_lock ()
{
Local dataId = "$ {1 }"
Local dataDate = "$ {2 }"
Local status = "$ {3 }"
Local SQL = "delete from t_trans_lock
Where dataId = '$ {dataId}' and dataDate = '$ {dataDate }';"
Echo $ {SQL} | $ {DB_PUBLIC}
If [$? -Ne 0]; then
Write_log $ {dataId} "free lock failed"
Fi
Return $ {status}
}
I hope this article will help you design the PHP + MySQL program.