Database transaction cross different connections

Is that possible?

try { start tran;

$this->db1->execute(sql1); $this->db2->execute(sql2);

commit tran; } catch (Exception $e) { rollback tran; throw $e; } finally { docleanup(); }

No. As far as the database is concerned, those two connections are two entirely different people. A transaction being started is a query that runs on that connection and will have no affect on your other connections. Only that connection will have the transaction started.

If you want to have the query on connection 1 be rolled back if the query on connection 2 fails, you're going to have to watch for that and manually roll back.



14.9k

I saw this, does it work? ```php $manager = new \Phalcon\Mvc\Model\Transaction\Manager();

$manager->setDbService("vs_hotel"); $tran1 = $manager->get();

$manager->setDbService("vs_common"); $tran2 = $manager->get();

$pointModel = new PointModel(); $pointModel->setTransaction($tran2); $pointModel->name = "ttt"; $pointModel->position = "[122, 3432]"; $pointModel->cityid = 323; $pointModel->citypy = "hello";

if($pointModel->save() == false) { foreach($pointModel->getMessages() as $message) { echo $message->_toString() . PHPEOL; } $manager->rollback(); }

$eqsModel = new EQSModel(); $eqsModel->setTransaction($tran1); $eqsModel->outid = "343"; $eqsModel->entityid = 3343; $eqsModel->vendors = "[fdfd]";

if($eqsModel->save() == false) { foreach($eqsModel->getMessages() as $message) { echo $message->_toString() . PHPEOL; } echo "=====---eqs rollback---======\n"; $manager->rollback(); } $manager->commit(); ```