2015-09-28 45 views
3

我使用PdoSessionHandler將用戶的會話存儲在數據庫中以使用會話Symfony2服務器和棘輪服務器進行通信。鎖定等待超時Symfony2棘輪與PdoSessionHandler

它連接OK,發送消息確定,但是當我在Symfony2應用程序中更改爲其他頁面或關閉會話時,它會調用onClose函數。然後應用程序被阻止,並返回以下錯誤:

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

的腳本樹枝頁面是:

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); 

我能做些什麼來避免鎖定?

+0

我在這裏有同樣的問題。這似乎是由Symfony團隊知道的:https://github.com/symfony/symfony/issues/4976 – Moonchild

回答

1

根據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 
); 
+0

完美! thans。現在它工作正常。 – Smarquina

+0

你的經驗出錯了嗎? – Taurus

+0

你的回答實際上是否被低估?因爲使用'new PDOSessionHandler($ con,['lock_mode'=> 0])'也解決了我的問題。 – Taurus