【问题标题】:Laravel error Lock database vs Transaction breakLaravel错误锁定数据库与事务中断
【发布时间】:2017-06-26 06:58:14
【问题描述】:

请任何人帮助我! 在我的项目中,我有锁定记录的交易:

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.

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

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

【问题讨论】:

    标签: php mysql laravel locking pessimistic


    【解决方案1】:

    如果您使用的是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();
            }
    

    【讨论】:

    • 当我取消命令时,``` php artisan test:run ... running step 1 ... ... running step 2 .. .. Ctrl C -> quit 命令。 ``` 事务未提交,锁未释放。所以,我更新数据库,记录锁定不可用。如何在强制退出命令 laravel 上提交事务
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-13
    • 1970-01-01
    • 2023-04-07
    • 2015-10-11
    • 1970-01-01
    • 2018-02-12
    • 1970-01-01
    相关资源
    最近更新 更多