2017-08-09 19 views
1

我的應用程序有一個窗體來管理用戶角色。在表單提交,當角色的變化中檢測到用戶的會話記錄已被從會話表中刪除: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 <...> 
+0

我不知道你的問題的原因,但我會建議你使用[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

回答

0

最後,我發現這奇怪的行爲僅適用於當前登錄的用戶。因此,對於當前用戶簡單註銷要做到:

if ($user->getId() == $this->getUser()->getId()) { 
    return $this->redirectToRoute('fos_user_security_logout'); 
} 
0
  • 多少會話行已在你的表有什麼?
  • 你的sessions.sess_id列有索引嗎?
  • 您是否嘗試使用mysql CLI或phpmyadmin運行此查詢?
  • 您是否在一些真實用戶使用的應用程序上測試了您的代碼,如果有的話 大概有多少?

請放下你的會話和用戶實體代碼來檢查關係

您的回答將幫助我回答你。

PS:很抱歉,但我不能因爲我的名聲寫評論(< 50)