# How can I use database transaction in cakephp3.4

**URL:** <https://discourse.cakephp.org/t/how-can-i-use-database-transaction-in-cakephp3-4/2791>\
**Category:** Need Help\
**Created:** [June 28, 2017, 5:38am UTC](https://discourse.cakephp.org/t/how-can-i-use-database-transaction-in-cakephp3-4/2791 "2017-06-28T05:38:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amol\_Chavan](https://avatars.discourse-cdn.com/v4/letter/a/ee59a6/32.png) [@Amol\_Chavan](https://discourse.cakephp.org/u/Amol_Chavan)\
**Post date:** [June 28, 2017, 5:38am UTC](https://discourse.cakephp.org/t/how-can-i-use-database-transaction-in-cakephp3-4/2791/1 "2017-06-28T05:38:32Z")

</div>

How can I use database transaction in cakephp3.4  
I have tried as per below:

$this-\>User-\>begin();  
$this-\>User-\>commit();  
$this-\>User-\>rollback();

but it throws error as “Unknown method “begin””

---

<div class="post-metadata">

**Author:** ![xaphalanx](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/xaphalanx/32/575_2.png) [@xaphalanx](https://discourse.cakephp.org/u/xaphalanx)\
**Post date:** [June 28, 2017, 12:49pm UTC](https://discourse.cakephp.org/t/how-can-i-use-database-transaction-in-cakephp3-4/2791/2 "2017-06-28T12:49:17Z")

</div>

If you are using 3.4, $this-\>Users-\>save($userEntity), is wrapped in a transaction already. If you want to control the transaction you can  
try {  
$this-\>Users-\>getConnection()-\>begin();  
$this-\>Users-\>saveOrFail($userEntity, [‘atomic’ =\> false]);  
$this-\>Users-\>getConnection()-\>commit();

} catch(\Cake\ORM\Exception\PersistenceFailedException $e) {  
$this-\>Users-\>getConnection()-\>rollback();  
}

Another possibility is found in the book.

[https://book.cakephp.org/3.0/en/orm/saving-data.html#converting-multiple-records](https://book.cakephp.org/3.0/en/orm/saving-data.html#converting-multiple-records)

}

---

<div class="post-metadata">

**Author:** ![Graziel](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Graziel](https://discourse.cakephp.org/u/Graziel)\
**Post date:** [June 29, 2017, 2:58pm UTC](https://discourse.cakephp.org/t/how-can-i-use-database-transaction-in-cakephp3-4/2791/3 "2017-06-29T14:58:40Z")

</div>

you can also use [transactional](https://book.cakephp.org/3.0/en/orm/database-basics.html#Cake%5CDatabase%5CConnection::transactional) which is more convinient

---

<div class="post-metadata">

**Author:** ![jarekgol](https://avatars.discourse-cdn.com/v4/letter/j/58956e/32.png) [@jarekgol](https://discourse.cakephp.org/u/jarekgol)\
**Post date:** [July 7, 2017, 7:28pm UTC](https://discourse.cakephp.org/t/how-can-i-use-database-transaction-in-cakephp3-4/2791/4 "2017-07-07T19:28:53Z")

</div>

On top of same page that Graziel write is:

```
use Cake\Datasource\ConnectionManager;
$connection = ConnectionManager::get('default');

```

so, try using -\>begin() on connection.
