DB Begin Transaction

Hi all experts,

I like to ask what is the difference using Yii::app()->db->beginTransaction() against ARmodel->dbConnection->beginTransaction()? Is there any concern if I use Yii::app()->db->beginTransaction(), it will lock certain table, row, or any?


I guess there is no difference, both are the same db-connection. But I don’t know about locking and so on.

The only difference is that you can override the model’s dbConnection when you extend CActiveRecord. For example it is technically possible to have all your AR classes use different DB connections.

So if you override the AR to connect to a different database(or use another connection to the same database) and you start a transaction with Yii::app()->db->beginTransaction() the subsequent changes made to the record will be out of this transaction, and will be executed immediately.

Generally if you want changes to a specific model to be in a transaction, you should use the models dbConnection to start the transaction, as this way if the model’s dbConnection will be changed later to something different than the application’s default connection, the changes will be still surrounded with a transaction.