Transaction Between Databases

Hi

I’m new in Yii Framework and it’s my first post in english, don’t laugh, please :P

I know, how i make transaction with other ActiveRecords, but how i can make that, when AR are in other databases?

Example :




//begin transaction

$activeRecordOne->save(); //in example is from db1

$activeRecordTwo->save(); //in example is from db2

$activeRecordThree->save(); //in example is from db3

//end transaction



I please for fast reply, it’s important for me :)

I think you need a third part tool to connect and execute transaction between

different databases. What db engine are you using?

MySQL

Read this:

It’s not a fully robust solution, but you could open separate transactions for each database and commit or roll them all back together.




    $t1 = Yii::app()->db1->beginTransaction();

    $t2 = Yii::app()->db2->beginTransaction();


    try

    {

        if (!$model1->save())

            throw new Exception;


        if (!$model2->save())

            throw new Exception;


        $t1->commit();

        $t2->commit();

    }

    catch (Exception $ex)

    {

        $t1->rollback();

        $t2->rollback();

    }



It’s not completely safe though because issues such as connection errors between the commits to the different databases will cause your databases to be in an inconsistent state.

EDIT:

In light of the double ninja posts above, as the databases are the same engine, Fabrizio’s suggestion makes the most sense. I assumed you were using different database systems.