Rollback() ineffective in mysql

Hi guys,
database operations first will be done after having set commit() method. This shows up, that transaction will do propper generally at my system. If I want to set back all operations in case of error, rollback is ineffective, operations won’t be set back. What’s wrong with this code?

function actionDelete($id) {
    try {
        $transaction = \Yii::$app->db->beginTransaction(); // etc..etc..

        $transaction->commit(); ...// etc..etc..
    } catch (\Exception $e) {
        $transaction->rollBack();// this is ineffective. Deletion won't be undone.Why?
        error_handling::error_without_id($e, MailController::RenderBackInCaseOfError);
    }
}

I bet you are modifying tables there, right? If so - the code is ok, that’s the MySQL fault. Read this - https://dev.mysql.com/doc/refman/5.7/en/implicit-commit.html

1 Like

What do U mean with MySQL fault. Maybe, I’m using wrong engine? Ur link doesn’t give any hints in terms of rollback deleted records.

SELECT TABLE_NAME,ENGINE
FROM   information_schema.TABLES
WHERE  TABLE_SCHEMA = 'yii2_kanatimmo';

will show InnoDB…

Are there any statements in your transaction that are on the list in my link? If so it means that MySQL immediately commits after executing them so transaction is not working like you want.

You need to start transaction outside try-catch block, like in doc example https://www.yiiframework.com/doc/api/2.0/yii-db-transaction

Coding
$transaction = \Yii::$app->db->beginTransaction();
before try block fixed issue. Greeting towards acidka, which helped me, fixing issue. This thread can be closed beeing solved succesfully.

Not necessarily, variables declared inside try block are available in its catch block.

@tklustig I’m glad it helped but it definitely was not a reason.

1 Like