How to get last running transaction Id in magento

How to get last running transaction Id in magento

September 12, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to get last running transaction Id in magento

Retrieving the Last Running Transaction ID in Magento

In Magento, retrieving the last running transaction ID can be crucial for various data analysis and auditing purposes. However, finding this information isn’t as straightforward as you might expect due to the intricate nature of Magento’s database schema.

Understanding Magento’s Transaction Management

Before we dive into the code, let’s briefly understand how transactions work in Magento:

  • Each time a resource (like a product or order) is saved or deleted, Magento starts an atomic transaction.
  • Multiple operations within this transaction are executed as a single unit. If any operation fails, the entire transaction rolls back.
  • Magento uses MySQL’s built-in transaction handling for these operations.

Retrieving the Last Transaction ID

To get the last running transaction ID (e.g., 10000001), you need to query the mysql_information_schema.tables view. Here, I’ll provide a sample PHP function that does exactly this:

public function getLastTransactionId()
{
    $resource = $this->_getResource();
    $select = $resource->getConnection()->select();

    $select->from('INFORMATION_SCHEMA.INNODB_TRX');
    $select->where('TRX_MYSQL_THREAD_ID NOT IN (SELECT CONNECTION_ID() AS TRX_MYSQL_THREAD_ID)');

    return $select->query()->fetchAll()[0]['TRX_ID'];
}

This function connects to the Innodb transaction system, selects all transactions that are not currently running by the current session (TRX_MYSQL_THREAD_ID NOT IN (SELECT CONNECTION_ID() AS TRX_MYSQL_THREAD_ID)), and then returns the TRX_ID of the last non-current transaction.

Remember to replace $this->_getResource() with your actual resource model or connection object. For example, if you’re working in a Magento 2 environment:

$resource = \Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Framework\App\ResourceConnection');

Best Practices and Troubleshooting Tips

  • Be careful when executing this query frequently, as it can impact database performance.
  • Consider caching the result for a certain duration if you’re not dealing with real-time data.
  • If you’re still facing issues, ensure your MySQL user has sufficient privileges to access the mysql_information_schema.tables view.

Conclusion

Retrieving the last running transaction ID in Magento requires a deep understanding of MySQL’s internal tables. By using the provided PHP function and following best practices, you can now easily obtain this information for auditing or data analysis purposes.

Remember, maintaining a healthy database is crucial for your e-commerce platform. If you’re struggling with performance issues or need expert advice on optimizing your Magento data workflows, don’t hesitate to reach out to our team of experienced developers at Laravel Company.

Catalog Database Orders

Our network