transactions have not been committed or rolled back

transactions have not been committed or rolled back

July 25, 2026 · Illustrated author avatarBy Stefan Bogdanescu
transactions have not been committed or rolled back

Resolving Uncommitted Transactions Errors in Magento

If you’re encountering “Some transactions have not been committed or rolled back” errors while importing data into your Magento database using custom scripts, this guide will help you understand the root cause and provide practical solutions to resolve the issue.

Understanding the Problem

When you execute SQL queries or save models in Magento without properly handling transactions, the system may leave some transactions open. This can lead to inconsistencies in the database and result in the “Some transactions have not been committed or rolled back” error when subsequent operations are performed.

Let’s analyze your provided code snippets:

  1. Direct Insertion:

    $write = Mage::getSingleton("core/resource")->getConnection("core_write");
    $write->insert("my_table", array("title" => 'test', "page_title" => 'test'));
    
  2. SQL Query with Connection:

    $sql = "INSERT INTO my_table (title, page_title) VALUES ('test', 'test')";
    $write->query($sql);
    
  3. Model Saving:

    $customModel->setTitle('test');
    $customModel->setPageTitle('test');
    // ...
    $result = $customModel->save();
    

In all these cases, while the insertions or savings are successful at an individual level, there is no explicit transaction management involved. This means that if any part of a larger operation fails after some data has been inserted or saved, the partially completed work remains in an uncommited state.

The Magento Transaction Lifecycle

Magento uses transactions to ensure data consistency and integrity across multiple database operations. By default, it automatically begins a new transaction for each request cycle (i.e., when a page is loaded or an action is dispatched).

However, during heavy batch processing tasks like data imports, Magento might not be able to manage the transactions efficiently due to the high volume of concurrent operations.

Solution 1: Use Magento’s Transaction Management Functions

Instead of directly interacting with the database connection object or model save method, you should leverage Magento’s built-in transaction management functions. This ensures that all related operations are wrapped within a single logical transaction:

$write = Mage::getSingleton('core/resource')->getConnection('core_write');
$transactionSave = Mage::getSingleton('core/resource')->getTransactionSavepoint();

try {
    $write->insert("my_table", array("title" => 'test', "page_title" => 'test'));
    $transactionSave->save();
} catch (Exception $e) {
    $transactionSave->rollBack();
    throw new Exception($e->getMessage());
}

Solution 2: Use the Resource Model and Transaction Helper

The Mage_Core_Model_Resource_Db_Abstract class provides a more robust way to handle transactions. You can use the save() method for your model, which automatically handles transaction management:

$customModel = Mage::getModel('my_module/my_model');
$customModel->setTitle('test');
$customModel->setPageTitle('test');

try {
    $customModel->save();
} catch (Exception $e) {
    throw new Exception($e->getMessage());
}

Solution 3: Implement Custom Transaction Management

If you need fine-grained control over transactions, especially for complex batch processing scripts, consider implementing your own transaction management logic using the Zend_Db_Adapter_Abstract object:

$write = Mage::getSingleton('core/resource')->getConnection('core_write');
$transaction = $write->beginTransaction();

try {
    // Your database operations here
    $write->insert("my_table", array("title" => 'test', "page_title" => 'test'));
    $transaction->commit();
} catch (Exception $e) {
    $transaction->rollBack();
    throw new Exception($e->getMessage());
}

Conclusion

Proper transaction management is crucial for maintaining data consistency and integrity in Magento, especially during bulk operations like data imports. By understanding the transaction lifecycle and applying the recommended solutions, you can effectively resolve “Some transactions have not been committed or rolled back” errors.

Remember to always test your changes thoroughly and monitor the database for any potential inconsistencies after implementing these fixes.

For more detailed information on Magento’s transaction management and best practices for data import scripts, consider exploring the resources available at LaravelCompany.

This post provides a comprehensive overview of the issue you’re facing and offers practical solutions to help you resolve it promptly.

Database Import/Export Errors

Our network