Welcome Guest, Not a member yet? Register   Sign In
Problem with transactions try catch
#1

I have a table that has a unique field, which I use to check that no identical insertions are made, this query is executed in the save_res method which is called within the transaction, as per the simplified example code.

Code in the model
PHP Code:
public function my_method($jsonData)
{
  try{
    $this->db->transBegin();

    // Some operations and queries...

    // Call to a method that executes some queries, including the one on the table in which a unique field exists
    $insert_action $this->save_res($jsonData);

    $this->db->transCommit();

    return $insert_action;  
  
} catch (DatabaseException $e) {
    // Log error
    log_message('error''My Error message: ' $e->getMessage());

    $this->db->transRollback();

    return ['error' => 'TEST ERROR'];
  }



Simulating an insertion with key duplication, I would expect that the transaction would fail and therefore all the queries would rollback and that my log message would therefore be written in the log file, but this does not happen, in the log file I find the system log reporting the uniqueness violation, but the catch part is never executed.

I also tried to transfer all the code of the save_res method into the my_method method, but nothing changes.

I don't understand why when the query that violates uniqueness goes into error, this doesn't trigger the catch part, or why it doesn't go into error even though the error is correctly reported in the log file.

Code:
ERROR - 2024-01-10 17:13:42 --> mysqli_sql_exception: Duplicate entry '2024-01-10-10:30:00' for key 'unique_data_time'
What's wrong?
Reply
#2

Try the code below using DatabaseException.

PHP Code:
<?php

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

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

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

(01-10-2024, 10:34 AM)InsiteFX Wrote: Try the code below using DatabaseException.

PHP Code:
<?php

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


I was just rereading the documentation and saw Since v4.3.0, during transactions, exceptions are not thrown by default even if DDBebug is true.

Thanks
Reply




Theme © iAndrew 2016 - Forum software by © MyBB