Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter 3 consecutive transactions not working

Some context first. I have a controller to manage sites. Sites can participate to a study. When a site participates to a study, an empty survey object is created. The participation has a link to that survey.

Simplified version of the controller code:

$survey = new Survey();
//Set some variables

$savedSurvey = $this->Surveymodel->persist($survey);

if ($savedSurvey) 
    $participation->survey_id = $savedSurvey->id; 

$savedParticipation = $this->Participationmodel->persist($participation);

The persist function is fairly straightforward and looks the same for all models.

$sql = "INSERT INTO table (a, b, c) VALUES (?, ?, ?)";
$params = array($object->a, $object->b, $object->c);

$this->db->trans_start();
$this->db->query($sql, $params);
$object->id = $this->db->insert_id();
$this->db->trans_complete();

if ($this->db->trans_status() === FALSE) {
    error_log('SURVEY INSERT KO');
    return false;
} else {
    error_log('SURVEY INSERT OK');
    return $object;
}

This is straightforward transaction use, exactly as described in the CI documentation. The PHP error log shows that the survey was inserted successfully and that the participation was also inserted successfully. However, the mysql logs paint a different story:

55 Query    START TRANSACTION
55 Query    INSERT INTO survey (survey_class_id, status, created_at) 
                   VALUES ('2', 1, NOW())
55 Query    COMMIT
55 Query    INSERT INTO participation (study_id, site_id, fs_folder_id, survey_id, reference, created_at) 
                   VALUES (1, '33', NULL, 49, 'REDVILLE', NOW())
55 Quit

Not only does the participation insert have no transaction start and no commit, but the code returns a successful operation (quite misleading) and the query without a transaction actually does not do the insert. There is no participation record in the table but the auto index is incremented. It's as if the second persist actually failed and there was a transaction start and a rollback that do not show in the mysql log. However if I manually copy the query in the mysql log and execute it, the insert works and the record is added to the table.

I have asked on the CI forums and searched for similar problems but haven't found anything.

  1. If I don't use transactions, it works (not a solution for me)
  2. I have tried both automatic CI transactions and manual ones with the same results.
  3. It isn't a model issue. If instead of inserting a survey and then inserting a participation I try to insert two participations, only the first one is actually added.
  4. I didn't have this bug in CI v2 which makes me wonder if it's a bug with the v3 but I find it unlikely that nobody would have ran into that issue before as it's a very basic operation.

I ran out of ideas to test why this doesn't work and could use some input from someone with an outside look and/or experience with the way CI3 handles transactions.

like image 214
Deratrius Avatar asked Jun 16 '26 03:06

Deratrius


1 Answers

Bug disappeared when upgrading my version of php from 5.5.10 to 5.6.10

In order to try to get a more accurate estimate of the version fixing the bug I tried PHP 5.5.22 which works (couldn't get anything between that and 5.5.10 for MAMP).

I checked the PHP ChangeLog and noticed this for version 5.5.12:

mysqli: Fixed problem in mysqli_commit()/mysqli_rollback() with second parameter (extra comma) and third parameters (lack of escaping).

Which doesn't link to any further details but is the only thing in the change logs that seems related to my issue and that matches the versions for which I have the bug (before 5.5.10 & after 5.5.22).

like image 150
Deratrius Avatar answered Jun 18 '26 21:06

Deratrius



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!