Use beginTransaction(); and commit(); in admin controllers in magento

Use beginTransaction(); and commit(); in admin controllers in magento

September 27, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Use beginTransaction(); and commit(); in admin controllers in magento

Handling Concurrent Invoices: Using beginTransaction() and commit() in Magento Admin Controllers

Hello! You’re facing an issue where multiple invoices are being created for the same order when performing a batch action from two browsers simultaneously. Let’s dive into solving this problem using transactions in your admin controller.

The Problem with Concurrent Actions

When you run the batch invoice and shipment action from multiple browsers, Magento doesn’t handle the concurrency properly by default. This leads to race conditions where both requests attempt to process the same orders at the same time, resulting in duplicate invoices.

Understanding beginTransaction() and commit()

In Magento 2, transactions are handled using the Mage_Core_Model_Resource_Transaction model and its methods: beginTransaction() and commit(). These allow you to create a transaction scope around your database operations. If an exception occurs within this scope, the changes will be rolled back; if everything succeeds, the changes are committed.

Setting up Transactions in Admin Controllers

To use transactions in your admin controller, you’ll need to include the resource model and handle it properly. Here’s how to do it:

  1. Include the Resource Model First, ensure you have included the resource model in your controller constructor or method:

    protected $transactionResource = null;
    
    public function __construct(
        ...
        Magento\Framework\App\ResourceConnection $resource,
    ) {
        ...
        $this->transactionResource = $resource;
        ...
    }
    
  2. Begin the Transaction Before processing your orders, begin a new transaction:

    try {
        $this->transactionResource->getConnection(Mage_Core_Model_Resource_Db_ABSTRACTION::DEFAULT_CONNECTION)->beginTransaction();
    } catch (Exception $e) {
        Mage::logException($e);
    }
    
  3. Process Orders within Transaction Scope Now, your order processing logic should be placed inside the try block:

    try {
        foreach ($orderIds as $orderIdss) {
            // Your existing code for loading orders and creating invoices
        }
    } catch (Exception $e) {
        Mage::logException($e);
        $this->transactionResource->getConnection(Mage_Core_Model_Resource_Db_ABSTRACTION::DEFAULT_CONNECTION)->rollBack();
        throw new Exception('An error occurred while processing orders: ' . $e->getMessage());
    }
    
  4. Commit the Transaction If no exceptions occur during the order processing, commit the transaction:

    try {
        $this->transactionResource->getConnection(Mage_Core_Model_Resource_Db_ABSTRACTION::DEFAULT_CONNECTION)->commit();
    } catch (Exception $e) {
        Mage::logException($e);
        throw new Exception('An error occurred while committing the transaction: ' . $e->getMessage());
    }
    

Conclusion

By following this approach, you ensure that all order processing happens within a single atomic operation. If an error occurs during the batch process (for example, if one of the orders cannot be invoiced), the entire transaction is rolled back, preventing any partial changes from being committed.

This way, even when running the action from multiple browsers simultaneously, only one set of changes will be applied. The others will either succeed or fail completely, maintaining data consistency in your Magento store.

Remember to thoroughly test this new implementation and monitor your logs for any potential issues that may arise.

Feel free to explore our other blog posts on Laravel Company for more tips and best practices on Magento development.

Magento 2 Database Admin Configuration

Our network