简体   繁体   English

在select上锁定mysql数据库表行

[英]lock mysql database table row on select

I am managing a pool of stateless session for a web service between users. 我正在管理用户之间的Web服务的无状态会话池。
So when a user request web service he start session and response timeout is 5 sec, so he can hold session for 5 sec max. 因此,当用户请求Web服务时,他启动会话并且响应超时为5秒,因此他可以保持会话最多5秒。 second user comes in and system check if there is available session then use it. 第二个用户进来并系统检查是否有可用的会话然后使用它。

Now i have a problem here. 现在我有一个问题。 when let say there is a session available, user A comes, system check if it was used more than 5 sec ago, given to user A, at the same time another user hit and system check if session used more than 4 sec ago, assigned to user B. 当让我们说有可用的会话时,用户A来,系统检查它是否超过5秒前使用,给用户A,同时另一个用户点击并系统检查会话是否超过4秒前使用,分配给用户B.
Now both user using same session and system fails on one. 现在,使用相同会话和系统的用户都失败了。

I have tried select of update command as well to lock it. 我已经尝试选择更新命令来锁定它。
I have tried update last used time as soon as it is selected by first user, but this didn't work (i think second user hitting system as the same time) 我已经尝试了第一个用户选择更新上次使用的时间,但这不起作用(我认为第二个用户同时击中系统)
can someone advice on it. 有人可以提出建议。

code: check for session from db if available then pick it or insert a new 代码:检查来自db的会话(如果可用)然后选择它或插入新的

//get 25 sessions from database order by lastqueryDate
$session = $sessionObj->select('session', '*', '', 'LastQueryDate DESC', '25');

$available_session = array();
//if sessions available, get rows from getResult
if ($session) {
    $session_Data = $sessionObj->getResult();
    //now get session which is sitting there more than response time out              
    $available_session = $sessionObj->getAvailableSession($session_Data);
}
//if there is any, use it. otherwise create new session and save in database
if (!$available_session) {
    $auth->securityAuthenticate();

    $header = $auth->getHeaders();
    $sequence = (int) $header['Session']->SequenceNumber + 1;
    $values[] = $header['Session']->SessionId;
    $values [] = $sequence;
    $values [] = $header['Session']->SecurityToken;

    $rows = "SessionID,SequenceNo,Security_token";
    if ($sessionObj->insert('session', $values, $rows)) {

        $available_session['Session']->SessionId = $header['Session']->SessionId;
        $available_session['Session']->SequenceNumber = $sequence;
        $available_session['Session']->SecurityToken = $header['Session']->SecurityToken;

    }
}

function that check availability of session in db : 检查db中会话可用性的函数:

public function getAvailableSession($session_data) {
    $available_session = array();
    foreach ($session_data as $key) {            
        if (!is_array($key)) {
            $key = $session_data;
        }


        $date = date('Y-m-d h:i:s a', time());
        $now = new DateTime($date);
        $last_query_time = new DateTime($key['LastQueryDate']);
        $dteDiff = $last_query_time->diff($now);
        $difference = $dteDiff->format("%H:%I:%S");
        //if response time out is smaller than session used last. then pick it.
        if (RTO <= $difference) {

            $available_session['Session']->SessionId = $key['SessionID'];
            $available_session['Session']->SequenceNumber = $key['SequenceNo'];
            $available_session['Session']->SecurityToken = $key['Security_token'];
            // run update to update lastqueryDate as its default value set to current time stamp
            $session_value = $key['SessionID'];
            $rows['SequenceNo'] = $key['SequenceNo'];
            $where[0] = "SessionID";
            $where[2] = "'" . $session_value . "'";                
            $this->update('session', $rows, $where);
            return $available_session;
        }
    }
    return false;
}

As soon as i find a session sitting idle for more than 5 sec i am updating database. 一旦我发现会话闲置超过5秒,我就会更新数据库。

Open transaction. 开放交易。 Issue "select ... for update" query for getting session data. 发出“select ... for update”查询以获取会话数据。 Commit transaction at end of script. 在脚本结束时提交事务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM