Yii Framework Forum: Transaction Between Databases - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Transaction Between Databases Rate Topic: -----

#1 User is offline   TickTak 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 28-March 14

Posted 28 March 2014 - 06:28 AM

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 :)
0

#2 User is offline   Fabrizio Caldarelli 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 285
  • Joined: 21-March 13
  • Location:Rome, Italy

Posted 28 March 2014 - 06:51 AM

I think you need a third part tool to connect and execute transaction between
different databases. What db engine are you using?
0

#3 User is offline   TickTak 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 28-March 14

Posted 28 March 2014 - 06:55 AM

MySQL
0

#4 User is offline   Fabrizio Caldarelli 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 285
  • Joined: 21-March 13
  • Location:Rome, Italy

Posted 28 March 2014 - 06:59 AM

Read this:

http://stackoverflow...mysql-databases
http://stackoverflow...nd-transactions
0

#5 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,520
  • Joined: 04-March 10
  • Location:UK

Posted 28 March 2014 - 07:00 AM

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.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users