PHP inserts mysql data to implement multi-host mutual lock instances

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.