2013-01-25 10:31
浏览 87

PHP set_time_limit然后重置数据库更改

I have an API which is used by a mobile app to create user profiles. It seems that occasionally the app is timing out and displays an error message but the server continues to create the profile. The result is that we have a few duplicate profiles.

I'm thinking that, as a 'quick fix' that doesn't require a complete re-architecture I could try setting the execution time of the PHP script to match the timeout limit in the app. My question is, if this time limit is reached, can I somehow execute an 'undo changes to the database' script, either automatic, or one I specify?

I know I should really be looking more into why the PHP is reaching timeout limit at all (it's a couple of minutes!), but I have already tweaked bits on the server, and mostly the execution time is just a few milliseconds. There are only a few cases of the timeout occurring. For the one example I'm looking at the minute the user is based in Israel. Is the network connectivity bad in Israel? Can anyone think of anything that could cause the behaviour I'm experiencing?

图片转代码服务由CSDN问答提供 功能建议

我有一个API,移动应用程序使用它来创建用户配置文件。 似乎偶尔应用程序超时并显示错误消息,但服务器继续创建配置文件。 结果是我们有一些重复的配置文件。

我认为,作为一个不需要完整重构的“快速修复”,我可以尝试设置PHP脚本的执行时间以匹配超时限制 该应用程序。 我的问题是,如果达到这个时间限制,我可以以某种方式执行“撤消对数据库的更改”脚本,无论是自动还是我指定的?

我知道我应该真的是 更多地了解为什么PHP完全达到超时限制(这是几分钟!),但我已经调整了服务器上的位,而且大多数执行时间只有几毫秒。 只有少数情况发生超时。 举个例子,我看着用户在以色列的这一分钟。 以色列的网络连接是否糟糕? 任何人都可以想到任何可能导致我遇到的行为吗?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dt1888 2013-01-25 10:38

    One approach would be to use database transactions. I have experience in this with PostgreSQL, but I am sure MySql will do that too. You would wrap everything in a transaction. Then you can cancel the transaction when you detect failure, rolling back everything which belongs to the transaction.

    Then, you can't catch a script timeout as it is a fatal error, however a transaction should be automatically rolled back (canceled) if it does not get committed, so you should be set...

    Maybe it could work for you?


    解决 无用
    打赏 举报
  • doujiang2641 2013-01-25 10:40

    Assumption: You're using MySQL w/ INNODB.

    You should look into using transactions. With INNODB, the transaction will rollback if the session times out IF you set autocommit to 0 like so:

    SET GLOBAL init_connect='SET autocommit=0';

    Example of transactions with PDO:

    try {
        // First of all, let's begin a transaction
        // A set of queries ; of one fails, an exception should be thrown
        $db->query('first query');
        $db->query('second query');
        $db->query('third query');
        // If we arrive here, it means that no exception was thrown
        // i.e. no query has failed ; and we can commit the transaction
    } catch (Exception $e) {
        // An exception has been thrown
        // We must rollback the transaction
    解决 无用
    打赏 举报

相关推荐 更多相似问题