1

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

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.

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

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

4

1 回答 1

0

如果您正在使用,DB::beginTransaction();那么您需要使用DB:commit();as

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

            //run step 1

            //run step 2

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

        } catch (\Exception $e) {
            DB::rollback();
        }
于 2017-06-26T07:05:11.020 回答