锁定等待超时 Symfony2 Ratchet with PdoSessionHandler
Lock wait timeout Symfony2 Ratchet with PdoSessionHandler
我使用 PdoSessionHandler 将用户的会话存储在数据库中,以便使用会话 Symfony2 服务器和 Ratchet 服务器进行通信。
它连接正常,发送消息正常,但是当我切换到 Symfony2 应用程序中的其他页面或关闭会话时,它会调用 onClose
函数。然后应用程序被阻止并且 returns 出现以下错误:
SQLSTATE[HY000]: General error: 1205 Lock wait timeout exceeded; try restarting transaction 500 Internal Server Error - PDOException
服务器看起来像:
$pdo = new PDO('mysql:host=localhost;dbname=XXXX', 'root', null);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbOptions = array(
'db_table' => 'sessions',
'db_id_col' => 'sess_id',
'db_data_col' => 'sess_data',
'db_time_col' => 'sess_time',
'db_lifetime_col' => 'sess_lifetime',);
$session = new PdoSessionHandler($pdo, $dbOptions);
$myApp = new ServerSocket();
$loop = \React\EventLoop\Factory::create();
$server = new \React\Socket\Server($loop);
$server->listen(8080, '0.0.0.0');
new IoServer(new HttpServer(new WsServer(new SessionProvider($myApp,$session))), $server);
echo "server running \n";
$loop->run();
"MyApp" 看起来像:
class ServerSocket implements MessageComponentInterface {
protected $players;
private $users;
public function __construct()
{
$this->players = [];
$this->users = new \SplObjectStorage();
}
function onOpen(ConnectionInterface $conn)
{
$this->users->attach($conn);
$this->players[$conn->Session->get('current_user_id')] = $conn;
print("new conection (". $conn->Session->get('current_user_id').")");
}
function onClose(ConnectionInterface $conn)
{
$this->users->detach($conn);
unset($this->players[$conn->Session->get('current_user_id')]);
$conn->close();
}
function onMessage(ConnectionInterface $from, $msg)
{
$data = json_decode($msg);
$to = $data->command;
if (isset($this->players[$to])) {
$this->players[$to]->send($data->message);
echo $data->message;
}
}
}
Twig 页面的脚本是:
var conn = new WebSocket('ws://localhost:8080');
conn.onopen = function(e) {
alert("Connection established!");
};
conn.onmessage = function(e) {
alert(e.data);
};
function sendMessage(msg,user) {
conn.send(JSON.stringify({command: user, message: msg}));
};
sendMessage("test",2);
我该怎么做才能避免锁定?
根据Symfony 2 blocked concurrency,禁用会话锁定已经解决了我的问题。
对于你的情况,你应该尝试:
$dbOptions = array(
'db_table' => 'sessions',
'db_id_col' => 'sess_id',
'db_data_col' => 'sess_data',
'db_time_col' => 'sess_time',
'db_lifetime_col' => 'sess_lifetime',
'lock_mode' => 0
);
我使用 PdoSessionHandler 将用户的会话存储在数据库中,以便使用会话 Symfony2 服务器和 Ratchet 服务器进行通信。
它连接正常,发送消息正常,但是当我切换到 Symfony2 应用程序中的其他页面或关闭会话时,它会调用 onClose
函数。然后应用程序被阻止并且 returns 出现以下错误:
SQLSTATE[HY000]: General error: 1205 Lock wait timeout exceeded; try restarting transaction 500 Internal Server Error - PDOException
服务器看起来像:
$pdo = new PDO('mysql:host=localhost;dbname=XXXX', 'root', null);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbOptions = array(
'db_table' => 'sessions',
'db_id_col' => 'sess_id',
'db_data_col' => 'sess_data',
'db_time_col' => 'sess_time',
'db_lifetime_col' => 'sess_lifetime',);
$session = new PdoSessionHandler($pdo, $dbOptions);
$myApp = new ServerSocket();
$loop = \React\EventLoop\Factory::create();
$server = new \React\Socket\Server($loop);
$server->listen(8080, '0.0.0.0');
new IoServer(new HttpServer(new WsServer(new SessionProvider($myApp,$session))), $server);
echo "server running \n";
$loop->run();
"MyApp" 看起来像:
class ServerSocket implements MessageComponentInterface {
protected $players;
private $users;
public function __construct()
{
$this->players = [];
$this->users = new \SplObjectStorage();
}
function onOpen(ConnectionInterface $conn)
{
$this->users->attach($conn);
$this->players[$conn->Session->get('current_user_id')] = $conn;
print("new conection (". $conn->Session->get('current_user_id').")");
}
function onClose(ConnectionInterface $conn)
{
$this->users->detach($conn);
unset($this->players[$conn->Session->get('current_user_id')]);
$conn->close();
}
function onMessage(ConnectionInterface $from, $msg)
{
$data = json_decode($msg);
$to = $data->command;
if (isset($this->players[$to])) {
$this->players[$to]->send($data->message);
echo $data->message;
}
}
}
Twig 页面的脚本是:
var conn = new WebSocket('ws://localhost:8080');
conn.onopen = function(e) {
alert("Connection established!");
};
conn.onmessage = function(e) {
alert(e.data);
};
function sendMessage(msg,user) {
conn.send(JSON.stringify({command: user, message: msg}));
};
sendMessage("test",2);
我该怎么做才能避免锁定?
根据Symfony 2 blocked concurrency,禁用会话锁定已经解决了我的问题。
对于你的情况,你应该尝试:
$dbOptions = array(
'db_table' => 'sessions',
'db_id_col' => 'sess_id',
'db_data_col' => 'sess_data',
'db_time_col' => 'sess_time',
'db_lifetime_col' => 'sess_lifetime',
'lock_mode' => 0
);