我的应用程序有一个表单来管理用户角色。在表单提交时,当检测到角色更改时,必须从会话表中删除用户的会话记录:
$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 user@localhost 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 <...>
答案 0 :(得分:0)
请删除会话和用户实体代码以检查关系
你的回答将帮助我回答你。
PS:很抱歉,但由于我的声誉(&lt; 50)
,我无法发表评论答案 1 :(得分:0)
最后我发现这种奇怪的行为仅适用于当前登录的用户。因此,对于当前用户,应该进行简单的注销:
if ($user->getId() == $this->getUser()->getId()) {
return $this->redirectToRoute('fos_user_security_logout');
}