Question

Im having troubles resolving the following problem. Lets assume I have this:

class UserMapper{
 private $Pdo;

 function __construct(PDO $Pdo) {
   $this->Pdo = $Pdo;
 }

 function DeleteUser(UserID){
   try{
     $Pdo->beginTransaction();
     // some logic here which eventually throws exception
     $Pdo->commit();
   }
   catch(Exception $e){
     // exception thrown from the upper code goes here
     // I need to log error with further details into some event log, e.g. like this
     $EventLog->LogEvent("error occured" . $e->getMessage()); // 
   }
 }

The problem with the code above is that if the exception is catched then the Pdo is currently in the "beginTranstacion" state and therefore the log is not updated in the database.

Well, this could be fixed by commiting it in the catch event, but then it would cause problems when there is no exception.

Is there any solution to this I might be missing?

Was it helpful?

Solution

You need to $Pdo->rollback() to revert the database changes in the catch block and tell your user that something wrong.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top