CodeIgniter Forums
Unable to check transaction status if a transaction fails - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Development (https://forum.codeigniter.com/forum-6.html)
+--- Forum: CodeIgniter 2.x (https://forum.codeigniter.com/forum-18.html)
+--- Thread: Unable to check transaction status if a transaction fails (/thread-63863.html)



Unable to check transaction status if a transaction fails - mr_pablo - 12-16-2015

I have a model method that does various inserts into various tables, and so I have wrapped it in a transaction.

However, I am struggling to log my own errors if the transaction fails, due to never being able to check $this->db->trans_status() when a legitimate DB error occurs.

After completing the transaction, I am checking for the status, as outlined here: https://ellislab.com/codeigniter/user-guide/database/transactions.html

But when a DB error occurs, for what ever reason (one that would cause the transaction to fail), my line where I check the status is never reached.

My code (simplified):

PHP Code:
$this->db->query('SIGNAL SQLSTATE \'45000\' SET MESSAGE_TEXT = \'Custom error\''); // this is to cause a DB error intentionally!

$this->db->trans_complete(); //complete the transaction, should rollback if anything fails right?

if($this->db->trans_status() === FALSE// This is never checked if a DB error occurs
{
 
   trigger_error("Commit failed"); // throw my own error, sadly never thrown :(
}
else
{
 
   return $object;




RE: Unable to check transaction status if a transaction fails - skunkbad - 12-16-2015

Are you starting the transaction?

$this->db->trans_start();


RE: Unable to check transaction status if a transaction fails - mr_pablo - 12-17-2015

(12-16-2015, 11:41 PM)skunkbad Wrote: Are you starting the transaction?

$this->db->trans_start();

Yes, I am using

PHP Code:
$this->db->trans_start(TRUE); // TRUE to enable test mode

// various inserts here

$this->db->query('SIGNAL SQLSTATE \'45000\' SET MESSAGE_TEXT = \'Custom error\''); // throw an error intentionally, for testing purposes!

$this->db->trans_complete();

if(
$this->db->trans_status() === FALSE// This is never reached, regardless of test mode or not, but it should be!
{
    
trigger_error("Commit failed"); // throw my own error, sadly never thrown :(
}
else
{
    return 
$object;


If you try this, even without test mode enabled, you will never reach the check for trans_status, because CI is intercepting the DB error right away, which is fine in a way.

I also tried using trans_begin, with the following check after the deliberate error:

PHP Code:
if($this->db->trans_status() === FALSE// putting a break point here, it never seems to hit it
{
    
$this->db->trans_rollback(); // break point here, never hit
    
trigger_error("Commit failed");
}
else
{
    
$this->db->commit();
    return 
true;


Although the transaction is never committed, I can't see how exactly it is being rolled back in this case (I have checked the DB and the data is not being inserted, as expected)

Sooo, it looks like checking for the trans_status is pointless in a production scenario, as any DB error is caught by CI/PHP anyway.

The only way it ever seems to check is when I set it to test_mode, then it always comes back as failed, which is ridiculous.

Test mode should not give a FALSE status, unless there is an error of course!


RE: Unable to check transaction status if a transaction fails - mr_pablo - 12-17-2015

Right, so I found one issue. If db_debug in the config file is set to TRUE, trans_status is never checked when there is a DB error, that gets spat out instead.

I set db_debug to FALSE, and trans_status is now being checked BUT not when transaction test mode is set to TRUE.

Is there anyway I can edit the DB driver to allow transactions to work, with test mode set to TRUE?


RE: Unable to check transaction status if a transaction fails - Narf - 12-17-2015

The manual that you've linked to is old (and no longer official, see the one at codeigniter.com instead), which leads me to believe you're using CI2.

The 2.x versions are no longer supported, so you should upgrade to CI3 anyway, but in addition to that - I remember fixing a number of transaction related issues in 3.0.3, so that might help.


RE: Unable to check transaction status if a transaction fails - mr_pablo - 12-17-2015

(12-17-2015, 05:29 AM)Narf Wrote: The manual that you've linked to is old (and no longer official, see the one at codeigniter.com instead), which leads me to believe you're using CI2.

The 2.x versions are no longer supported, so you should upgrade to CI3 anyway, but in addition to that - I remember fixing a number of transaction related issues in 3.0.3, so that might help.

Can you confirm that in CI3, transactions in test mode return TRUE (if valid) when checking the transaction status?

(Also, do transactions work in test mode? in CI2 they were fundamentally broken for ages, needing a system file edit to work)

NB - I am going to look at moving to CI3 over the coming week