Answer the question
In order to leave comments, you need to log in
BD transactions in Laravel. What is the behavior in different situations?
One way to use transactions in Laravel:
DB::beginTransaction();
// великолепный полезный код
DB::commit();
DB::beginTransaction();
// начало великолепного и полезного кода
Exeption или упаси бог Fatal Error где-то в процессе
//конец
DB::commit();
DB::beginTransaction();
// начало великолепного и полезного кода
//вызов восхитительной ф-ции, внутри которой тоже вызывается DB::beginTransaction(),
//а потом DB::commit()
func(...);
//конец
DB::commit();
DB::beginTransaction();
try {
DB::insert(...);
//.....
DB::commit();
// all good
} catch (\Exception $e) {
DB::rollback();
// something went wrong
}
Answer the question
In order to leave comments, you need to log in
I can’t speak for Laravel (I don’t use it), but most likely these functions call the corresponding database commands (BEGIN / START TRANSACTION / COMMIT etc.). The behavior will, generally speaking, depend on the database.
For example, if the code does not reach DB::commit();.
If somewhere in the process DB::beginTransaction() is called again - DB::commit()...
If DB::commit(); no one was going to call at all?
in general, any transaction will be rolled back unless an explicit commit is received.
there are exceptions.
autocommit times. operations that always commit a transaction - two.
smoke mans your subd.
in the case of nested transactions, everything works up to the last branch, except for operations that immediately commit the entire hierarchy of transactions.
Do not use manual transaction level control. The exception is specific situations, where it will be clear that it is needed anyway.
Answering the question, DB::transaction(callable) will rollback the transaction on an exception, the rest of the (manual) control does nothing automatically, except for handling a few specific errors that reset the transaction level themselves.
In general, it’s better not to go into manual control, because it’s VERY easy to get through, it’s difficult to debug, and Lara doesn’t calculate everything.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question