使用php重试数据库异常 [英] Retry on database exception using php

查看:155
本文介绍了使用php重试数据库异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在处理由php - mysqli&创建下面的这个代码片段,尝试检查几次以查看临时故障是否导致数据库失败。如果是这样,它会尝试恢复。如果不能,那么它会退出/结束。

I have been working on exceptions thrown by php - mysqli & created this snippet below that tries to check a few times to see if a temp glitch has caused the database to fail. if so, it tries to recover. If it cannot then it quits/ ends.

现在,当逻辑很好的时候,我找不到为什么echo语句不会按照它们定期发生。只有结束(取决于在findMail()函数中抛出异常的情况),输出被发送&正确的抛出新异常行可以随时更改为失败或通过最后一次。它被设置为通过片段中的最后一轮。如果有人可以指向我,为什么回显不会按期显示输出,那将是很棒的。

Now while the logic is fine, I cannot find out why the echo statements do not occur at regular intervals as they should. Only towards the end (depending upon how the exception is thrown in the findMail() function), the output is spewed & correctly so. The throw new exception line can be changed to fail at all times or to pass the last time around. It is set to pass the last time round in the snippet. Would be great if someone can point to me why the echo is not displaying the output as it should at intervals.

此外,我不知道这是一个好的还是一个生产中的坏主意。对此的任何反馈都非常受欢迎。感谢所有。

Further I am not sure if this is a good or a bad idea in production. Any feedback on this is very welcome. Thanks to all.

代码片段:

<?php
$host        = 'localhost';
$user        = 'root';
$password    = '';
$database    = 'test';

// OPEN A CONNECTION TO THE DATA BASE SERVER AND SELECT THE DB

try
{
    $con = new mysqli($host,$user,$password,$database);
    if($con->connect_error)
    {
        throw new Exception("Goof Up");
    }   
    mysqli_set_charset($con, "utf8"); 
}
catch(Exception $e)
{
    echo $e->getMessage();
    exit;
}

// All well so far 

$cnt = 0;
if(findMail($con,$cnt)) echo "<br> Hurray !!";

function findMail($con,$cnt)
{
    try
    {   
        echo $cnt++."<br>";
        $query = "SELECT name, email from users";
        $stmt=$con->prepare($query);
        if($cnt < 3 ) throw new exception($cnt);
        if($stmt->execute())    
        {   
            $stmt->bind_result($name, $email);
            while ($stmt->fetch())
                {
                    $email = htmlspecialchars($email);
                    $name = htmlspecialchars($name);
                    echo $name.' ---- '.$email.'<br>'; 
                }       
        }
    }
    catch(Exception $e)
    {
        $cnt = (int)($e->getMessage());
//      echo $cnt;
        if($cnt === 4) { echo "Ending"; exit();} 
        sleep(5);
        findMail($con,$cnt);
    }   
    return true;
}
?>


推荐答案

在使用浏览器渲染时,它必须与之相关在某些情况下,ob_start和ob_flush。

It must have something to do with browser rendering while using ob_start and ob_flush under certain conditions.

我注意到当我设置php 头(Content-Encoding:none)禁用gzip压缩:

I noticed when i set php header("Content-Encoding: none") to disable gzip compression:

更新 - 添加了一个示例

<?php 
header("Content-Encoding: none"); 

ob_end_clean();
ob_start(); 

for($i=0;$i<5;$i++) 
{ 
    echo 'No. '.$i."<br>";
    ob_flush();
    flush();
    sleep(2);
}
?>

它正确刷新。查看以下链接的更多详细信息

it flushes properly. Check out more details on the below links

PHP流/输出缓冲不再工作

对于php flush - 如何禁用特定文件的gzip

这篇关于使用php重试数据库异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆