CodeIgniter Forums
Code igniter transaction don't rollback on SQL error - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Code igniter transaction don't rollback on SQL error (/showthread.php?tid=67980)



Code igniter transaction don't rollback on SQL error - Esthar - 05-04-2017

Hi there,


today i've encountered a curious behaviour using CI transactions, calling a series of update queryies.


The strange thing is that the transaction not rollback when one of queryies return a sql error, (like an unexcistent field in a table), the other queryies  before the one that is throwing the error are executed normally.

The only thing i see is a sql error on screen.

my code is similar to this:

$this->db->trans_start();

$this->db->update(...);
$this->db->update(...);
$this->db->update(...);

$this->db->trans_complete();

echo ($this->db->trans_status === false) ? 'Rollback' : 'Ok';

Thank you


Someone knows why it not rollback as expected?


RE: Code igniter transaction don't rollback on SQL error - InsiteFX - 05-04-2017

You should be using an if else statement to check for an error.

PHP Code:
if ($this->db->trans_status() === FALSE)
{
 
       $this->db->trans_rollback();
}
else
{
 
       $this->db->trans_commit();




RE: Code igniter transaction don't rollback on SQL error - Esthar - 05-04-2017

But standing to the documentation, if i use $this->db->trans_start() & $this->db->trans_complete(), the queryies will rollback automatically when one of those fail.

Why my records are affected the same when one query fails?

I've noticed the engine of the table affected by the transaction was MyIsam, and than i changed it to InnoDB, ( because MyIsam not support transactions), but the results are the same. The queryies never rollback on a failure state.