Database write "beyond" active transaction - possible?


Presume I have a pretty complex database transaction being processed, but I need to execute a single write command in the middle of it, and this one should be excluded from the transaction, so it should be never rolled-back.

What is the right way to achieve this result?

Please notice, the table concerned must be an InnoDB table.

Thanks ahead,

Can’t you just break the database transaction into two and execute your write command on the basis your first half executes successfully, then continue on with the remainder of your transaction?

Not really, as these are “nested” entries: only after a record in a “parent” table is created - a respected “child” record in a related table can be created, etc.

You can clone Connection, and use cloned object to run these queries - it should have separate DB connection outside of transaction:

$clonedDb = clone Yii::$app->db;
$clonedDb->createCommand()-> ...

Note that writes performed using $clonedDb will not be visible inside of transaction.

1 Like


And this is the right idea - thank you!