PHP implements multi machine interlock instances by inserting mysql data

  • 2021-07-24 10:38:26
  • OfStack

This article describes the example of PHP through the insertion of mysql data to achieve multi-machine interlocking method, shared for your reference. The specific implementation method is as follows:

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

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 the execution fails or ends

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 is helpful to everyone's PHP + MySQL programming.


Related articles: