如果事务处理失败,如何恢复/回滚之前执行的查询?
我需要在事务中删除文件,是否有可能知道如果unlink文件失败,那么意味着删除文件失败,然后回滚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;
我想这就是你要找的:
if( is_file($file_path) ) {
if( ! unlink($file_path) ) { // or if(unlink($file_path) == false) {
$connect_db->rollBack();
$message = '';
return $message;
exit;
}
}
只要你的数据库支持事务。
您可能需要阅读事务文档