andrewalkermo's avatar

What happens when i don't rollback or commit a transaction?

I have a simple doubt. If i use DB::beginTransacion, and then DB::commit() when success, but no DB::rollback(), in case fails. What happens with the transaction when the application fails? Laravel will auto rollback the transaction for me if an exceptions is thrown? Someone told me that, but i can't confirm this information any where.

0 likes
3 replies
jlrdw's avatar

I am pretty sure if you don't use rollback then after so many tries and exception will be thrown anyway.

Niush's avatar

Yes, Laravel instance will disconnect the DB, and the database service will automatically rollback uncommitted transactions.

andrewalkermo's avatar
andrewalkermo
OP
Best Answer
Level 1

@Niush I found the answer in documentation after all. If i use DB::transaction, Laravel will do the rollback for me. But with DB::beginTransaction() i need to do DB::roolback() by myself.

https://laravel.com/docs/9.x/database#database-transactions

If an exception is thrown within the transaction closure, the transaction will automatically be rolled back and the exception is re-thrown. If the closure executes successfully, the transaction will automatically be committed.

1 like

Please or to participate in this conversation.