Zend Framework Database Transaction - Cannot Rollback

Ok, let me make it out first. My question is like this: Unable to roll back transaction in Zend Framework

My tables are always innoDB, always. I checked the table in question and it is really innoDB. To the question ..

I have a database instance, and model instances going through this transaction end up in the same database:

$db->beginTransaction(); try { // Run an insert $model_record->insert(array('single_item' => 'its value')); // More logic, and run an update. $model_record->this_value = 'that'; // Save it $model_record->save(); //Commit the transaction $db->commit(); } catch (Exception $e) { // It finds the rollback, yet does nothing. $db->rollBack(); } 

Now, the reason I didnโ€™t work is to exceed the limit of characters in the string during the test to make sure that all the logic in place is correct.

He is not a rollback. In addition, an entry in "single_item" was in the database. But the updated values โ€‹โ€‹were not.

I completely lost something small, I never had problems with transactions with MySQL and innoDB. Could this be due to MySQL or ZF? Any insight is helpful, thanks.

Update:

I did some more tests, and here are some results that may help:

  $this->_db->beginTransaction(); // This works $this->_db->insert('table_a', array( 'a_field' => 'transaction test', ) ); // This does not work, at all. It inserts and does not rollback. There is no commit. $_table_a_model->insert( array( 'a_field' => 'transaction test', ) ); $this->_db->rollback(); 

Additional update You need to get an instance of the model and invoke the transaction.

 $the_model = $this->_model->getAdapter(); $the_model->beginTransaction(); 

This leaves no room for transactions for multiple tables, without making multiple transactions for each instance of the model. Any ideas out there without returning to the underlying database instance?

+6
source share
2 answers

I get it. I have to use $db = Zend_Db_Table_Abstract::getDefaultAdapter(); and then complete my transactions so that all operations in multiple models work together in the same transaction. If anyone has a fix, feel free to comment.

+4
source

Perhaps your test case, exceeding the maximum characters, simply truncates the entered data and does not actually throw an exception? Assuming that if you do something like mistype, the table name will throw an exception (unless PDO :: ERRMODE_EXCEPTION is definitely checked). If you really raise an exception that blocks the lock, then it should fire and roll back, if the commit is fired instead, and you never call rollback (), it looks like the result should be expected.

Oh, and looking at your code, we need to make sure that $ db is the same instance in your model class as on this controller. Take a look here and see how the entire $ db descriptor is used.

Edit: @ We figured it out. "I need to use $ db = Zend_Db_Table_Abstract :: getDefaultAdapter () and then run my transactions so that all operations in several models work together under one transaction. If anyone has a fix, feel free to comment."

+1
source

Source: https://habr.com/ru/post/925055/


All Articles