Unable to check transaction status if a transaction fails |
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-gu...tions.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!
Are you starting the transaction?
$this->db->trans_start(); (12-16-2015, 11:41 PM)skunkbad Wrote: Are you starting the transaction? Yes, I am using PHP Code: $this->db->trans_start(TRUE); // TRUE to enable test mode 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 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!
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?
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. (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. 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 |
Welcome Guest, Not a member yet? Register Sign In |