CodeIgniter Forums
DB transaction error peculiarity - 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: DB transaction error peculiarity (/showthread.php?tid=88612)



DB transaction error peculiarity - codeus - 10-05-2023

Code:
Configuration:
CI v4.4.1
PHP v8.1.23
Ubuntu 22.04 LTS Jammy
mysql  Ver 15.1 Distrib 10.6.12-MariaDB

The transaction fails, but there's no indication in $this->db->error(). All the mariaDB tables are INNODB tables.


Code:
$this->db->transStart();
$this->db->query('INSERT'....);
$id = $this->db->insertID();
$this->db->query('UPDATE'....);
...
$this->db->transComplete();

if ($this->db->transStatus() === FALSE) {
    error_log('transaction error:' . print_r($this->db->error(), true));
    return false;
}


And in the log, there are numerous examples from the logging code like this:

Code:
PHP message: transaction error:
    code => 0
    [message] =>

Added db->error() to report on each individual SQL statements, each report indicates code is 0.
Is db-error() the wrong means to detect a transaction error, or is there actually an error?


RE: DB transaction error peculiarity - InsiteFX - 10-05-2023

Did you try using try / catch with Exceptions?

PHP Code:
// When DBDebug in the Database Config must be true.

use CodeIgniter\Database\Exceptions\DatabaseException;

try {
    $this->db->transException(true)->transStart();
    $this->db->query('AN SQL QUERY...');
    $this->db->query('ANOTHER QUERY...');
    $this->db->query('AND YET ANOTHER QUERY...');
    $this->db->transComplete();
} catch (
DatabaseException $e) {
    // Automatically rolled back already.




RE: DB transaction error peculiarity - codeus - 10-07-2023

Using exceptions, it works ok. The only thing changed in the SQL was one of the queries was db->simpleQuery(), changed that to db->query().
Will see if it still causes problem reverting back to non-exception code.