Yii2 Mariadb transaction is submitted successfully, but no data is saved in the database.
P粉386318086
P粉386318086 2024-03-30 23:03:30
0
1
470

I have a code:

$tx = Yii::$app->db->beginTransaction();
try {
  // CODE (a lot of active-record reads and writes)
  $tx->commit();
  echo "All good!";
} catch (\Throwable $ex) {
  $tx->rollback();
  echo "Error";
}

It executes and I get "Everything is fine!" hence. However, nothing is actually stored in the database. The code has been running for several months now without modification. It suddenly stopped working yesterday.

After an hour of debugging, I can confirm that the operation is working properly and I can "echo" the intermediate results, including the ID of the record I want to insert. But after the final submission, nothing is saved in the database.

If I remove the transaction, the code works and everything is stored in the database, just like it used to with transactions.

I want to ensure the integrity of the changes and want to go back inside the transaction. Or, at least, I'd like to understand what code (or database state or whatever) is preventing the data from being stored, and why I'm not getting any exceptions and echoing "error" because the transaction couldn't commit.

I was sure that the commit() method would throw an exception if the transaction commit failed (actually writing to the database), but this is not the case. Is there any way to get it?

Thank you very much in advance.

P粉386318086
P粉386318086

reply all(1)
P粉715304239

The problem lies in the code. Thanks, @Michal Hynčica, you are indeed right. There is a part like this:

foreach (..) {
  $tx = Yii->$app->db->beginTransaction();
  if ($oneRareCondition) {
    continue;
  }

  ...
  $tx->commit();
}

Solved this rare occurrence by adding $tx->commit(); before continue; if:

foreach (..) {
  $tx = Yii->$app->db->beginTransaction();
  if ($oneRareCondition) {
    $tx->commit();
    continue;
  }

  ...
  $tx->commit();
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template