SoFunction
Updated on 2025-03-09

PHP implements multi-machine interlocking instances by inserting mysql data

This article describes the method of PHP to implement multi-machine interlock by inserting mysql data. It is shared with you for your reference. The specific implementation method is as follows:

You can add a general lock before executing the process. The shell shackle function is as follows. If successful, it will return 0, otherwise it will return a non-0 value:

Copy the codeThe code is as follows:
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 $?
}

Release the lock when execution fails or ends

Copy the codeThe 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 be helpful to everyone's PHP+MySQL programming.