Database Transactions

In case you still need to use multiple database connections - e.g. when syncing data between different database servers - there is a helper that can be used in place of DB::transaction():

$result = db_transactions(['mysql', 'other-connection'], function() {
    // Make changes here...

    // Optionally return something to be assigned to $result
    return $something;
});

A transaction will be started in each specified database connection. If an exception is thrown within the closure, all of the transactions will automatically be rolled back and the exception will be re-thrown. If the closure executes successfully, all of the transactions will automatically be committed.

Warning: This doesn't guarantee consistency between the databases, as it is possible for an exception to be thrown when committing a transaction - resulting in some transactions being committed while others are rolled back.