Home > Backend Development > PHP Tutorial > PHP通过插入mysql数据来实现多机互锁实例_PHP

PHP通过插入mysql数据来实现多机互锁实例_PHP

WBOY
Release: 2016-05-31 19:28:21
Original
863 people have browsed it

本文实例讲述了PHP通过插入mysql数据来实现多机互锁的方法,分享给大家供大家参考。具体实现方法如下:

在执行流程之前可以加一般锁,shell枷锁函数如下,如果成功则会返回0,否则返回非0值:

代码如下:

function get_lock()
{
    local dataId="${1}"
    local dataDate="${2}"

    local sql="insert intot_trans_lock
    (dataId, dataDate) values('${dataId}', '${dataDate}');"
    echo ${sql} | ${DB_PUBLIC}

    return $?
}

在执行失败或者结束的时候释放锁

代码如下:

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}
}

希望本文所述对大家的PHP+MySQL程序设计有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template