1

I'm planing to use Try/Catch and Database Transaction in Laravel5 and my own PHP project to validate any errors occurs then rollback avoiding lost my transaction or information during user do their transaction to Database. I'm sure to find out if some method done with success status or non success status but I don't understand much yet about Try/Catch with Database transaction specially with BeginTransaction() and Commit(). Rollback() buildin method in Larvel5. I have done with this function but finally I don't know how many errors type will happen during those transaction. When and how catch find those (if error happen) errors and return to users? In Return under DB::commit() method I will return all status to end user but how about catch do their task if error happen and what kind of those errors. Example:\

public function my_method(){
DB::beginTransaction():
try {
    $this->data['a'] =  $this->select();
    $this->data['b'] =  $this->delete();
    $this->data['c'] =  $this->update();
    $this->data['d'] =  $this->insert();
DB::commit();
return ['data'=>$this->data];
       //I planing return all the return back from each method to user.
} catch(EXCEPTION $e){
           DB::rollback()
            throw $e
           }
}

private update(){ if(done){return true}}
private delete(){ if(done){return true}}
private insert(){ if(done){return true}}
private select(){ if(done){return true}}

closed as off-topic by Ixrec, Adam Zuckerman, user22815, gnat, Jim G. May 27 '16 at 8:25

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions asking for assistance in writing or debugging existing code are off-topic on Programmers. These questions can be asked on Stack Overflow if they include the desired behavior, a specific problem or error, and the shortest code necessary to reproduce it in the question itself. See How To Create a Minimal, Complete, and Verifiable Example." – Ixrec, Adam Zuckerman, Community
If this question can be reworded to fit the rules in the help center, please edit the question.

4

First off, even though PHP is class case insensitive I would rewrite EXCEPTION to Exception as it's easier to read. Also, you're missing a few semi colons. I would fix those few things first!

I'm not very familiar with the the Laravel database classes, but I assume you're using Eloquent? If so you may be able to catch the \Illuminate\Database\QueryException exception in your try catch block and handle the rollback that way. Something like this should work:

    try {
        // do your database transaction here
    } catch (\Illuminate\Database\QueryException $e) {
        // something went wrong with the transaction, rollback
    } catch (\Exception $e) {
        // something went wrong elsewhere, handle gracefully
    }

https://laravel.com/api/5.2/Illuminate/Database/QueryException.html

If you're using straight up PDO then I would recommend trying the same strategy as above but for the PDOException exception instead.

http://php.net/manual/en/class.pdoexception.php

  • That okk, But is it work or not if I used the method instead of using eloquent or Laravel query builder or PDO inside of Try? and those method are alway return a value or true false after it complete it's task. – Heng Sopheak May 19 '16 at 11:08
  • Inside your method as long as it throws the right type of exception it will get caught by your try catch block else where. You do not neccessarily need to catch the exception inside the same method is was thrown. You can catch an exception in any method that calls your original method. – user229034 May 19 '16 at 11:23
  • Here is an example of what I mean: 3v4l.org/pFm1q – user229034 May 19 '16 at 11:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.