我的應用程序有一個窗體來管理用戶角色。在表單提交,當角色的變化中檢測到用戶的會話記錄已被從會話表中刪除:Symfony刪除實體永遠不會刷新
$form = $this->createForm('AppBundle\Form\UserType', $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$user->setRoles($this->setUserRoles($user));
$changed = $this->checkChanged($em->getUnitOfWork()->getOriginalEntityData($user), $request->request->get('app_user_profile'));
if ($changed) {
// $sql = "DELETE FROM sessions WHERE sess_id = '".$user->getSessId()."'";
// $em->getConnection()->exec($sql);
// $em->getConnection()->commit();
$entity = $em->getRepository('AppBundle:Sessions')->find($user->getSessId());
if ($entity) {
$em->remove($entity);
// $em->flush($entity);
}
$user->setSessId(null);
}
$em->persist($user);
$em->flush();
return $this->redirectToRoute('users_list');
}
但是這個代碼的執行導致異常SQLSTATE[HY000]: General error: 1205 Lock wait timeout exceeded; try restarting transaction
我評論了,我已經與嘗試代碼相同的結果。 MySQL的日誌文件顯示刪除命令後10秒的停頓,我不知道它在等待,爲什麼連$em->getConnection()->commit();
這裏是行不通的:
2017-08-09T10:02:36.334195Z 217 Connect [email protected] on mydb using TCP/IP
2017-08-09T10:02:36.336556Z 217 Query SELECT t0.username AS username_1, <...> FROM fos_user t0 WHERE t0.id = 1 LIMIT 1
2017-08-09T10:02:36.373640Z 217 Query DELETE FROM sessions WHERE sess_id = '1gjo6har0vttn7ru63pjrptti4'
2017-08-09T10:03:27.489764Z 216 Query INSERT INTO sessions (sess_id, sess_data <...>
我不知道你的問題的原因,但我會建議你使用[PdoSessionHandler(https://symfony.com/doc/current/ doctrine/pdo_session_storage.html)將會話存儲在數據庫中,然後實現[EquatableInterface](http://api.symfony.com/3.3/Symfony/Component/Security/Core/User/EquatableInterface.html),以在用戶角色改變。 – DrKey