How can i test a transaction?

$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.