# How can i test a transaction?

**URL:** <https://discourse.cakephp.org/t/how-can-i-test-a-transaction/8037>\
**Category:** Testing\
**Created:** [July 9, 2020, 1:04pm UTC](https://discourse.cakephp.org/t/how-can-i-test-a-transaction/8037 "2020-07-09T13:04:24Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![Julian](https://avatars.discourse-cdn.com/v4/letter/j/74df32/32.png) [@Julian](https://discourse.cakephp.org/u/Julian)\
**Post date:** [July 9, 2020, 3:55pm UTC](https://discourse.cakephp.org/t/how-can-i-test-a-transaction/8037/4 "2020-07-09T15:55:47Z")

</div>

```
$this->MyModel->query("START TRANSACTION");//i have already used the transaction functions too

try{
foreach(){
//...data proccess
 $this->MyModel->save($myData);
 
 foreach(){
  //...some data process
  $this->MyModel->assocciatedModel->save($data);
 }
}

}catch(Exception){
 $success = false;
}

if($success){
 $this->MyModel->query("COMMIT");
}else{
 $this->MyModel->query("ROLLBACK");
}

```

I have code like this, im saving some data but my transaction isn’t working.

---

_[View the full topic](https://discourse.cakephp.org/t/how-can-i-test-a-transaction/8037)._
