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
-
Please explain your question further. What kind of transaction? What are you trying to accomplish?– Ryan HoerrCommented May 26, 2016 at 4:01
Add a comment
|
1 Answer
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();
}
}
}