4

In magento 2, How to provide transaction ?
Anyone knew, please show me step by step.
I found a solution for M1.x here How Provide transaction In Magento

1
  • Please explain your question further. What kind of transaction? What are you trying to accomplish?
    – Ryan Hoerr
    May 26 '16 at 4:01
7

I resolved my issue. below:

<?php
namespace Odoo\Office\Helper;

use Magento\Framework\App\ResourceConnection;
/**
* 
*/
class DbConnection
{
    /** 
     * Define common connection
     * @var unknown
     */
    protected $_resource;
    protected $_dbConnection;

    protected function __construct(
        ResourceConnection $resource,
        )
    {
        # code...
        // Set common connection
        $this->_resource        = $resource;
        $this->_dbConnection    = $this->_resource->getConnection();
    }

    public function Trans(){
        try{
            // Start transaction
            $this->_dbConnection->beginTransaction(); 

            //........

            // Commit transaction
            $this->_dbConnection->commit();
        } catch (\Exception $e) {
            // Rollback transaction
            $this->_dbConnection->rollBack();
        } 
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.