Php 带异常的PDO事务

Php 带异常的PDO事务,php,exception,pdo,transactions,Php,Exception,Pdo,Transactions,我在解决以下问题时遇到困难。假设我有: class UserMapper{ private $Pdo; function __construct(PDO $Pdo) { $this->Pdo = $Pdo; } function DeleteUser(UserID){ try{ $Pdo->beginTransaction(); // some logic here which eventually throws exception

我在解决以下问题时遇到困难。假设我有:

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()); // 
   }
 }
上面代码的问题是,如果捕获到异常,则Pdo当前处于“BeginTranstation”状态,因此数据库中的日志不会更新

好吧,这可以通过在catch事件中提交来解决,但是如果没有异常,它会导致问题


是否有我可能缺少的解决方案?

您需要$Pdo->rollback()来还原catch块中的数据库更改,并告诉用户有问题。

错误。。。我说得对吗,你试图将日志保存到刚才失败的同一个数据库中?
函数DeleteUser(UserID)
参数不是变量?是的,我只是手动键入了整个代码,所以在这里键入:)是的,保存到同一个数据库,但没有问题,因为错误不是异常,但是我的习惯性例外表明有些事情是错误的,我想你是对的。。我怎么以前没想到,我一定是工作过度了。。