Zend Framework Database Transaction - Can't rollback

Alright, let me get this out of the way first. My question is similar to this: Cannot rollback transaction in Zend Framework

My tables are always innoDB, always. I've checked the table in question and it is indeed innoDB. On to the problem..

I have a database instance, and model instances running through this transaction hitting 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 found this to not be working is I exceeded the character limit on a row during a test to make sure all the logic in place was correct.

It did not rollback. On top of that the record with 'single_item', was in the database. But the updated values, were not.

Am I completely missing something small, I've never had transaction issues with MySQL & innoDB. Could this be MySQL related or ZF related? Any insight is helpful, thanks.

Update:

I've been conducting a few more tests and here are a few results that might 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 the instance of the model, and call a transaction on that.

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

This leaves no room for transactions for multiple tables, without doing multiple transactions for each model instance. Any ideas out there without reverting to the base database instance?

Answers


I figured it out. I have to use $db = Zend_Db_Table_Abstract::getDefaultAdapter(); and then run my transactions off that so all operations in multiple models work together under a single transaction. If anyone has a fix for that, feel free to comment.


Maybe your test case, exceeding the max chars just truncates the data inserted and does not in fact raise an exception? Assuming that if you do something like mistype the name of a table you will get an exception (if not definitely check PDO::ERRMODE_EXCEPTION is enabled). If you are truly raising an exception that catch block should fire and rollback, if the commit is instead firing and you never call rollback() it sounds like the result is what you should expect.

Oh, and looking at your code suppose we should make sure that $db is the same instance in your model class as on that controller. Look here and see how same $db handle is used throughout.

Edit: @Wes figured it out. "I have to use $db = Zend_Db_Table_Abstract::getDefaultAdapter(); and then run my transactions off that so all operations in multiple models work together under a single transaction. If anyone has a fix for that, feel free to comment."


Need Your Help

How to programmatically select popup menu item in Windows?

ui-automation postmessage popupmenu trackpopupmenu wm-command

I have an app that I'm writing a little wizard for. It automated a small part of the app by moving the mouse to appropriate buttons, menus and clicking them so the user can watch.

UITableview background changes while searching

iphone objective-c ios uitableview

i am developing an app contains an UITableView and a search bar. I set clear color to the background of the the UITableView, but while i am searching an item in the search bar the TableView backgro...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.