Automatically rollback transaction on try catch construction

61 Views Asked by At

I'm using the try catch construction like

try {
    foo();
    bar();
} catch(SomeException $exception){
    throw new SomeException('some exception');
}


foo()
{
    // sql query 'INSERT INTO .... execute query
}

bar() 
{
    try{
        //some logic
    } catch(SomeException2 $exception){
        throw new SomeException2('some exception2');
    }
}

Case 1:

When executing the bar() method I get an exception 'SomeException2' and record in MYSQL table is rolled back like as if I used a transaction rollback

If i try...

Case2

try {
    db->startTransaction();
    foo();
    db->commitTransaction();
    bar();
} catch(SomeException $exception){
  
    throw new SomeException('some exception');
}

my record is saving in database.

Does catch rollback the transaction in Case #1?

0

There are 0 best solutions below