【问题标题】:Delete file if fail during transaction, how to recover/rollback query execute before如果在事务期间失败删除文件,如何恢复/回滚查询之前执行
【发布时间】:2014-06-18 19:19:02
【问题描述】:

如果在事务过程中失败删除文件,如何恢复/回滚之前执行的查询?

我需要删除事务中的文件,是否可以知道如果取消链接文件失败则意味着删除文件失败然后回滚之前执行的sql..

如果在unlink()之后执行查询失败,如何恢复之前删除的文件?
比如让 unlink() 脱离事务

try{
  $connect_db->beginTransaction();
  // execute select query
  // execute delete query
  // .. execute other query    

  if (is_file($file_path)) {
      if(unlink($file_path) == false) {
          // How to recover/rollback delete query and other query execute before

          $message = '';
          return $message;
          exit;
      }
  }

  //  ....execute other query
  $connect_db->commit();
} catch (PDOException $e) {
  $message = '';
}

return $message;

【问题讨论】:

    标签: php mysql sql pdo transactions


    【解决方案1】:

    我想这就是你要找的:

    if( is_file($file_path) ) {
          if( ! unlink($file_path) ) { // or if(unlink($file_path) == false) {
              $connect_db->rollBack();
    
              $message = '';
              return $message;
              exit;
          }
      }
    

    只要您的数据库支持事务。

    您可能想read the transaction documentation

    【讨论】:

    • 感谢您的回复!如果 unlink() 之后执行查询失败了怎么恢复之前删除的文件?
    • 您是在问unlink() 是否可以参与交易?
    • 它不会成为 PDO 事务的一部分。
    • ??通常人们是怎么做的呢?我应该怎么做才能让它工作?
    • 文件包含什么?
    猜你喜欢
    • 1970-01-01
    • 2019-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-15
    • 1970-01-01
    • 1970-01-01
    • 2014-03-22
    相关资源
    最近更新 更多