失败时两次运行查询 [英] Running Query twice in case of failure

查看:75
本文介绍了失败时两次运行查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我总是检查MySQL查询是否成功,如果不成功则再次运行它们(而不是显示或记录错误等)

I always check if MySQL Queries are successful and run them again if not (instead of displaying or logging errors etc)

有时候,对于重要的查询,我什至将它们置于有限的While循环中,以防失败时尝试5-10次

Sometimes for important queries, i even put them in a limited While loop to try for 5-10 times in case of failure

例如:

$update = $db->update("table", ["today" => $date]]);

if ($update === FALSE OR $affected_rows < 1)
{
    // Optional sleep(1);
    $update = $db->update("table", ["today" => $date]]);
}

if ($update === FALSE OR $affected_rows < 1)
{
    // Now log if the second try failed too...
}

但是今天我在考虑这样做是否正确,也许甚至已经有一个函数可以重试,而不是在代码中键入相同的查询两次(因此,如果我们更改一个查询,我们必须记住要更改其他)

But today I was thinking if this is a right thing to do, and maybe even there is already a function to retry instead of typing the same query twice in the code (so if we change one We have to remember to change the other)

  1. 这是对的事吗?有什么缺点吗? (我的意思是,如果成功,它不会运行两次)

  1. Is this a right thing to do? any downsides? (i mean it doesn't run twice if it is successful)

是否有一种更简单的方法(不要两次使用相同的代码)?我要复制粘贴两次相同的查询,如果我更改了第一个查询而忘记更改了第二个查询,则可能很危险(我使用的是Meo,它是PDO包装器)

Is there a simpler way to do this (not using the same code twice)? I'm Copy-Pasting the same query twice, and if I change the first and forget to change the second it can be dangerous (I'm using Meedo which is a PDO wrapper)

P.S. I know the second won't work if the MySQL is down etc, i do it for temporary Failed attempts or Aborts or anything else that may not happen on the second try)

推荐答案

很显然,您在这里追求的是被设计打破"的策略.而不是弄清楚为什么为什么以前的尝试没有成功,而是(盲目地……)只是再次尝试". (而且,显然,在您的开发环境"的象牙塔中,它还没有 [yet(!)] 失败了两次"! ,它将.)

Obviously, you are pursuing a strategy here that is "broken by-design." Instead of figuring out why the previous attempt did not succeed, you are (blindly ...) "just trying it again." (And, apparently, in the ivory-tower of your "development environment," it has not [yet(!)] "failed twice!" But, sooner or sooner, it will.)

您需要一劳永逸地做到这一点".

You need to "get to the bottom of this, once and for all."

为什么(?!)第一次尝试失败?有什么合理的理由应该这样做吗? (我什么都看不到...)因此,仅使问题(似乎)消失"就不够了".您必须解决它.

Why(?!) is the query failing on its first attempt? Is there any plausible reason why it should? (I see none ...) Therefore, it is not good-enough to "merely make the problem (seem to) 'go away.'" You have to solve it.

在共享数据库中使用SQL更新事物"时,通常需要使用事务". (而且,在MySQL环境中,这意味着您必须使用"InnoDB" 表.)BEGIN TRANSACTION,然后进行一系列更新(或插入,删除,等等). ,则您选择COMMITROLLBACK.

When you are "updating things" in SQL, in a shared database, you ordinarily need to use "Transactions." (And, in the MySQL environment, that means that you must use "InnoDB" tables.) You BEGIN TRANSACTION, then you make your series of updates (or inserts, deletes, whatever ...), then you either COMMIT or ROLLBACK.

这篇关于失败时两次运行查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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