Framework: DB::transaction does not work when using DB::connection()

Created on 21 Jun 2017  路  3Comments  路  Source: laravel/framework

  • Laravel Version: 5.4.#
  • PHP Version: 7.1.5
  • Database Driver & Version: MySQL 5.6.36

Description:

DB::transactions do not work when using DB::connection()

Steps To Reproduce:

Create a non-default config for a mysql database.

Attempt to run the following code:

````
DB::beginTransaction();

try {

    DB::connection('newdb')->table('users')->where('id', 2)->update(['status' => 'VALIDATED']);
    throw new \Exception('Some new exception');
    DB::commit();

} catch ( Exception $ex ){

    DB::rollBack();

}
````

The DB transaction is immediately committed at the update statement and can not be rolled back.

Most helpful comment

Start the transaction on the same connection your query will run on.

All 3 comments

Start the transaction on the same connection your query will run on.

Duh... Thank you!

@themsaid thank you very much. I think this specific point with multi database connections could be added in documentation.
Can I help for that @taylorotwell ?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

RomainSauvaire picture RomainSauvaire  路  3Comments

JamborJan picture JamborJan  路  3Comments

Fuzzyma picture Fuzzyma  路  3Comments

jackmu95 picture jackmu95  路  3Comments

progmars picture progmars  路  3Comments