1

Anyone help me, please!. In my project, i have transaction with lock record:

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();
        }
    }
}

in my case, i call it from command, when it running:

php artisan test:run

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

-> quit command.

Transaction not commit and lock record forever.

Can i commit transaction on command force quit?

huy hoang
  • 43
  • 3

1 Answers1

0

If you are using DB::beginTransaction(); then you need to commit using 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();
        }
Rahul
  • 2,374
  • 2
  • 9
  • 17
  • when i cancel command, ``` php artisan test:run ... running step 1 ... ... running step 2 .. .. Ctrl C -> quit command. ``` The transaction not commit, Lock not release. So, i update database with record locked not available. How can commit transaction on force quit command laravel – huy hoang Jun 26 '17 at 09:46