CodeIgniter Forums
Cancelling current transaction started with $this->db->trans_start() - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Cancelling current transaction started with $this->db->trans_start() (/showthread.php?tid=60386)



Cancelling current transaction started with $this->db->trans_start() - El Forum - 03-14-2014

[eluser]teomor[/eluser]
I'm using $this->db->trans_start() to start a transaction and $this->db->trans_complete() to commit the changes, but what if I want to cancel the whole thing in the middle of it? Can I just use $this->db->trans_rollback()? Would that work on the whole transaction group?
The documentation only says to use $this->db->trans_rollback() when manually starting transactions with $this->db->trans_begin(). I'm a bit confused if this would work or not..


Cancelling current transaction started with $this->db->trans_start() - El Forum - 03-15-2014

[eluser]letsgolee[/eluser]
if I'm right, you can rollback all, I think though I have not tested yet.

Transactions are started when $this->db->trans_start() is calling, and it can be called as many as you want.
if something is wrong and you want rollback all then we must know what depth of transaction and the depth is recorded using $this->db->_trans_depth. So the rollback function must be called as many as the nested transactions.

Code:
$this->db->trans_start();
...
$this->db->trans_start();
...

if (/* something is wrong and you want roll back all */) {
while ($this->db->_trans_depth) {
  $this->db->trans_rollback();
  $this->db->_trans_depth--;
}
}



Cancelling current transaction started with $this->db->trans_start() - El Forum - 03-18-2014

[eluser]teomor[/eluser]
Right. So as long as I call trans_rollback as many times as I call trans_start I should be OK, that's what your're saying, correct? Smile
As it happens, that's exactly what I am doing..