Welcome Guest, Not a member yet? Register   Sign In
Code igniter transaction don't rollback on SQL error
#1

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?
Reply
#2

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();

What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(This post was last modified: 05-04-2017, 05:48 AM by Esthar.)

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




Theme © iAndrew 2016 - Forum software by © MyBB