Welcome Guest, Not a member yet? Register   Sign In
Catching Duplicate entry exceptions while executing transactions
#1

[eluser]ufasoli[/eluser]
Hi everyone,
I'm having problems understanding how exceptions are handled in php (I'm quite new to this language). Let me explain my problem :

There is an update query that sometimes needs to be executed in my application, and every once in a while I can end up with a duplicate entry database error, so I was wondering if there was a way to catch this exception then alter the database query so I don't get an error and then continue with my other queries, something like

Code:
$this->db->trans_begin();
        
try
{
        
     $this->db->update('travailler',
         array('id_etude'=>$new_id_etude), array('id_etude'=>$id_etude));    
            
    if($this->db->trans_status() === false)
    {
      $this->db->trans_rollback();
     $msg =  "alert(\"Un probl\\350me est survenu lors de l'op\\351ration\"); ";
        
    }
    else
    {
      $this->db->trans_commit();
        
    }
            
}
catch(Excepion e)
{
  // some code here
}


My question would be, after I've caught the exception how do I manage (if possible) to execute a different query and then continue where I encountered the problem ?? I realize it's a bit too much that I ask but maybe you have an idea or an alternative way to handle this....
#2

[eluser]dioony[/eluser]
Hi!

Actually i was searching for a good workaround to catch such database errors... no matter what...

i hope I've gotten it right:

You're problem:
you have a lot of queries in a transaction e.g. q1,q2,q3,q4...
now, if an exception will be raised in q2, you want to catch this, send an alternative query only for q2 (or none) and after that carry on with q3 and q4.
is that right?

normally if a transaction failed the database runs itself a rollback to revert all queries before the error encountered. i never used transactions with CI before, but normally you don't have to rollback manually...

if I'm right with the example on the top: this would argue against the transaction-concept, because a transaction should be closed action. means: either all achieves success or one query failed and all failed. so, if it will failed you have do to a complete new transaction...


or is you're problem where to put the alternative query in case of an exception? (thus: the process in a try-catch-block if an exception will be raised)



sorry for my English Wink




Theme © iAndrew 2016 - Forum software by © MyBB