5

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
    Commented May 26, 2016 at 4:01

1 Answer 1

9

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 and acknowledge you have read our privacy policy.

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