PDOException: there is no active transaction

EDIT: i forgot to add i just upgraded to version 2.0.2. Maybe this has something to do with it?
I have two database connections on my project. I tried to use an isolated transaction in a function but it threw the error mentioned in the title ("there is no active transaction").
By the way, i did not have this problem before, but it might be due to the fact the last time i used this method there was no second connection.
I did this example intentionally simple and naive, the code is actually quite a bit longer and obviously does not involve cats.

<?php

try{
    $transaction = $this->getDI()->getTransactions()->get();
    $person = new \Person();
    $person->setTransaction($transaction);
    //do other code stuff
    //....
    if(!$person->save()){
        $transaction->rollback('Human could not be saved');
        //it will not think there is any active transaction at this point
        //and will instead throw an error
        }
    $cat = new \Cat();
    $cat->setTransaction($transaction);
    $cat->setWhiskerLength(1000);
    if(!$cat->save()){
        $transaction->rollback('The kitty could not be saved');
        //it will not think there is any active transaction at this point
        //and will instead throw an error
    }
    $transaction->commit();
catch(Failed $e){
    echo $e->getMessage();
}

Both database connections are set $di->setShared(connection name)

I have no clue how to solve this problem. It used to work well.
Any ideas? Thanks in advance



41.7k
edited Jun '15

Your problem is that if the record cannot be saved then you roll it back and then the very next line you try to commit it.

instead make it:

    if(!$cat->save()){
        throw new \Exception('');
    }
    $transaction->commit();

} catch(\Exception $e){
    $transaction->rollback();
    echo $e->getMessage();
}

I don't write code that looks like that but you'll get the idea.