2017-06-26 97 views
1

任何人都可以帮助我! 在我的项目,我有事务以锁定记录:Laravel错误锁定数据库vs事务处理中断

namespace App\Console\Commands; 

use Illuminate\Console\Command; 

class Testing extends Command { 
    protected function handling() 
    { 
     DB::beginTransaction(); 
     try { 
      $posts = Posts::lockForUpdate()->find(1); 

      //run step 1 

      //run step 2 

      //run step 3 

     } catch (\Exception $e) { 
      DB::rollback(); 
     } 
    } 
} 

在我的情况,我把它从命令,它运行时:

php artisan test:run 

... 
running step 1 
... 
... 
running step 2 
.. 
.. 
Ctrl C 

-> quit command. 

事务未提交,并永远锁定记录。

我可以在命令强制退出时提交事务吗?

回答

0

如果您正在使用DB::beginTransaction();那么你需要使用DB:commit();作为

DB::beginTransaction(); 
     try { 
      $posts = Posts::lockForUpdate()->find(1); 

      //run step 1 

      //run step 2 

      //run step 3 
      DB::commit(); 

     } catch (\Exception $e) { 
      DB::rollback(); 
     } 
+0

当我取消命令, ''' PHP工匠测试承诺:运行 ... 运行第1步 .. ... 运行步骤2 .. .. Ctrl C - > quit命令。 ''' 事务没有提交,锁不释放。所以,我更新数据库记录锁定不可用。 怎样才能强制执行交易quit命令laravel –