2

I tried to make a db transaction when I made multi queries.

and also I tried to catch the exceptions outside the transaction just like

DB::transaction(function() {
   try{
        //....db queries
   }catch(\Exception $e){
        Log::info($e);
   }
});

when some exceptions thrown here, the transaction seems like not be rolled back, the data has been changed in the database.

If I do DB::rollBack() in the catch statement manually, all my data will be fine,

it makes me think if I have to do DB::rollBack() manually?

but I do remember the doc of Laravel said if I am using DB::transaction to manage my db queries, when the exceptions threw, DB::rollBack() will be executed automatically?

It has been changed in Laravel 5.3?

Abel Lee
  • 55
  • 1
  • 10
  • also show your proper code to make your question more clear – msonowal Jan 11 '17 at 06:54
  • @msonowal I just wanna discuss the logic of the DB::transaction, not an exact code. it's easy to understand what i am talking about if you do know Laravel, so I don't think I have to type so many code to make my point clear. – Abel Lee Mar 06 '17 at 20:44

4 Answers4

4

Follow this link. you shouldn't use DB::transaction() but instead wrap your code in DB::beginTransaction and DB::commit/DB::rollback()

Hope this work for you!

Community
  • 1
  • 1
AddWeb Solution Pvt Ltd
  • 21,025
  • 5
  • 26
  • 57
  • so whether I should use the DB::beginTransaction rather than DB::transaction? If yes, when the DB::transaction will be used? seems like the DB::transaction is unnecessary. – Abel Lee Jan 11 '17 at 07:38
  • @AbelLee: If you would like to begin a transaction manually and have complete control over rollbacks and commits, you may use the `beginTransaction` method, [Docs](https://laravel.com/docs/5.3/database#database-transactions). – AddWeb Solution Pvt Ltd Jan 11 '17 at 07:42
  • I just wanna catch the exceptions in the transaction, also the origin behaviors of the transaction will be executed. cause of not all of the transactions are right absolutely. If so, the DB::transaction is not so better than DB::beginTransaction – Abel Lee Jan 11 '17 at 07:49
2

If You have multiple table entries and update depending on each Other .it is advised that you should use Transactions

$var_name = DB::transaction(function () {
//    DB operations.....
});

$var_name returns null when successful

for further details please refer to the doc https://laravel.com/docs/5.3/database

Hope this Helps you. Ask in case any query

Rohit shah
  • 833
  • 4
  • 15
  • thanks in advanced, I have read the doc, but I think the doc is not so detailed. It just told me how to use DB::transaction and the DB::rollBack() will be executed in a normal transaction way but I have did try...catch manually, they never mentioned. – Abel Lee Jan 11 '17 at 07:11
  • you might be getting a different error because i myself was working on db transaction and i didnt need try catch..please check once – Rohit shah Jan 11 '17 at 07:13
  • some normal situation just 500, I do know this. I just wanna know whether I have to do DB::rollBack() manually when I wrapped a "try...catch" outside the db queries. – Abel Lee Jan 11 '17 at 07:14
  • The reason why I got the error, cause of I haven't submit a non-nullable column to the mysql, If I do not wrap a "try...catch", it will show me a db error but 500 error for the frontend when I was in the debug mode. – Abel Lee Jan 11 '17 at 07:18
  • I am using Laravel 5.3, I found if I don't rollback manually, it will be some weird performance in mysql, I guess whether cause of the transaction is multi-thread? because I found I if do "try...catch" in the transaction, all queries will be executed no matter what any exceptions or not. the id column will be updated sometimes or some entried will be deleted but one entry will be saved if I do deletion and creation queries together in the transaction. – Abel Lee Jan 11 '17 at 07:26
0

it will not be executed automatically if you're implementing like that, but if you use it as a closure like below

DB::transaction(function () {
    //db queries
});

It will be rollbacked automatically.

msonowal
  • 1,553
  • 3
  • 17
  • 36
  • I have tried that like you have posted, I even don't know the DB::rollBack() whether be excuted, it just throw out a "500" error code if some exceptions occurred in the DB::transaction. so I wanna wrap a "try...catch" to make the exceptions comfortable. And if I do this, I found I have to DB::rollBack() manually :( – Abel Lee Jan 11 '17 at 07:03
0

The proper way with transactions and try/catch is like this:

try {
    DB::transaction(function(){
        //your query stuff here
    });

    DB::commit(); //transactions had no error
} catch (\Exception $e ) {
    //transactions had an error
    DB::rollback();

    //do something with $e->getMessage();
}

The purpose is to give you greater control in the event that your queries fail. This way, you can catch the exception and roll the transaction back. Otherwise, you'd just perform the transaction.

Ohgodwhy
  • 49,779
  • 11
  • 80
  • 110