What Causes DatabaseTransactionOutOfOrderException and how do I fix this? I am working on plugin to Drupal Commerce, and I started getting this error: 

DatabaseTransactionOutOfOrderException: in DatabaseConnection-&gt;rollback() (line 1058 of /Library/WebServer/Documents/includes/database/database.inc).


Can anyone shed some light on what this error means? If I install a fresh drupal and reinstall my plugin, is that likely to clear things up? 

Any advice? 

Thanks!
The DatabaseTransactionOutOfOrderException exception is part of the rollback() method (DatabaseConnection class) and it is thrown when a rollback() resulted in other active transactions being rolled-back instead of rollback to correct earlier savepoint.

You may debug it checking the backtrace when this exception is thrown.

In example defining the following hook:

/**
 * Implements hook_watchdog().
 */
function foo_watchdog($log_entry) {
  if ($log_entry['type'] == 'php' &amp;&amp; $log_entry['severity'] &lt;= WATCHDOG_WARNING) {
    // Old school
    // var_dump(debug_backtrace()); // Optionally add: exit();

    // Devel: Log the backtrace into temporary file: drupal_debug.txt
    // Locate via: $ drush eval "echo file_directory_temp() . '/drupal_debug.txt'"
    function_exists('dd') &amp;&amp; dd(debug_backtrace());
  }
}


Don't forget to install Devel module (for dd) and clear the cache before testing it.

Then try to reproduce the problem and then check your /tmp/drupal_debug.txt debug file which should contain something like (section _drupal_exception_handler):

[function] =&gt; _drupal_exception_handler
[args] =&gt; Array
    (
        [0] =&gt; DatabaseTransactionOutOfOrderException Object
            (
                [file:protected] =&gt; /var/www/includes/database/database.inc
                [line:protected] =&gt; 1071
                [trace:Exception:private] =&gt; Array
                    (
                        [0] =&gt; Array
                            (
                                [file] =&gt; /var/www/includes/database/database.inc
                                [line] =&gt; 1933
                                [function] =&gt; rollback
                                [class] =&gt; DatabaseConnection
                            )
                        [1] =&gt; Array
                            (
                                [file] =&gt; /var/www/sites/all/modules/contrib/commerce/modules/order/includes/commerce_order.controller.inc
                                [line] =&gt; 123
                                [function] =&gt; rollback
                                [class] =&gt; DatabaseTransaction
                            )
                        [2] =&gt; Array
                            (
                                [file] =&gt; /var/www/sites/all/modules/contrib/commerce/modules/order/commerce_order.module
                                [line] =&gt; 734
                                [function] =&gt; save
                                [class] =&gt; CommerceOrderEntityController
                                [type] =&gt; -&gt;


So based on the above example, you know that something goes wrong in commerce_order.module on line 734 during CommerceOrderEntity save.

If it's still not clear, other suggestion is to enable MySQL query logging and check the suspicious queries which are sent.