【问题标题】:php to freeze before committing transaction?php 在提交事务之前冻结?
【发布时间】:2015-01-10 21:10:01
【问题描述】:

我希望 php 在提交事务之前冻结,以便在提交事务之前向用户显示更新日志并提供继续或回滚的选项。

这是将更新提交到数据库的代码:-

<?php
include 'submitLogger.php';

// Begin logging
ini_set( "error_log", $logFile );
ini_set( "log_errors", "On" );
ini_set( "display_errors", "Off" );
error_log( "Log file '" . $logFile . "' created" );

// Open the database
.
.

error_log( "Connect OK" );
.
.

error_log( "Transaction started (autocommit OFF)\n" ); 
.
.

error_log( "Processing " . count( $deletes ) . " item(s) marked for deletion..." ); 
.
.


// commit changes
error_log( "Committing changes..." ); 
if ( mysqli_commit( $link ) === false ) {
  mysqli_rollback( $link );
  error_log( "Commit failed. Transaction rolled back." ); 
  $response['error'] = "Could not commit changes. Transaction rolled back.";
} else {
  error_log( "Commit successful!" ); 
  $response['success'] = "Success!";
}

// close DB connection
.
.

// Return result
.
.

这是加载上面更新代码然后显示日志文件的代码,但是在事务提交之后:-

<script type="text/javascript">

$(document).ready( function() {


  // send AJAX request to perform the updates and begin logging
  $.post(
    '../lib/updateMenu.php', 
    sendData,
    function( response ) {
      // was it successful?
      if ( typeof response.success === 'undefined' ) {
        // no - show alert
        if ( response.error ) {
          alert( response.error );
        }
        console.error( "Amend not successful" );
        console.error( response );
        return;
      }

      // delete the AmendmenuAmendselected program window to force a reload on next click
      $( "#programWindowAmendmenuAmendselected" ).remove();
    },
    "json"
  ).error(function(jqXHR, textStatus, errorThrown) {
    alert( 'Unexpected error: ' + textStatus + ' ' + errorThrown );
    console.error( 'Unexpected error during amend: ' + textStatus + ' ' + errorThrown );
    console.error(jqXHR);
  }).complete(function() {
    // once a reply is received, stop the logging
    ajaxLogtail.stopTail();      
  });

  // begin querying log file
  var ajaxLogtail = new AjaxLogtail( '../lib/ajaxLogtail.php?logfile=' + logFile, "programWindowAmendmenuSubmit" );  
  ajaxLogtail.startTail();
});

</script>

如何让 php 代码在提交事务之前等待前端的响应?

请问有人有什么好主意可以帮忙吗?

【问题讨论】:

  • 不可能。这不是http的工作方式。您不能“冻结” php 然后恢复,也不能在用户提示需要的单个 http 请求中执行客户端服务器之间的多次往返
  • @Marc B,我可以运行两次交易吗?第一次作为诱饵向用户显示日志,第二次是真实的,如果用户选择不显示日志?很乱,有什么好办法吗?
  • 并非如此。当一个 php 脚本退出时,将执行清理并且您的数据库连接将被关闭并且事务回滚。如果您尝试使用持久数据库连接,则无法保证来自用户的响应会获得相同的连接,并且您最终会得到一个完全不同的连接,其中没有等待事务。

标签: php jquery logging transactions commit


【解决方案1】:

由于您目前尝试的方式无法实现,因此我会为您提供解决问题的方法。

我会让我的脚本运行一个外部进程,或者更确切地说是一个持久进程或工作者(例如齿轮人)。它将由工作人员创建事务并向我的主脚本返回响应以向用户提供反馈。

需要记录工作实例,以便以后继续。当用户确认后,请求的下一次执行将重新连接到告诉它提交或回滚的工作人员。

需要注意的是事务的预期寿命,如果用户等待的时间过长,与数据库的连接可能会关闭。

【讨论】:

  • 听起来正是我想做的@Flosculus。你能给我一个很好的链接,看看如何设置这个工人还是我应该只用谷歌“gearman”?
  • php.net/manual/en/book.gearman.php 我建议的实现并不像听起来那么简单,除非您熟悉这个概念。试试 gearman,先不要尝试实现这个,只是在程序上练习玩 RPC 方面,然后你就会明白如何得到你需要的东西。
  • 我已经进行了一些调查,但 gearman 无法正常工作,至少有两个原因:1. 它是 php 到 php 架构,2. 我的更新等是随机的,无法在工作中定义因此。但感谢@Flosculus 提供的信息
  • PHP 连接到 gearman,因此您在命令行中运行 PHP,该脚本打开与 gearman 的连接并等待。然后其他脚本连接到 gearman 并请求完成一项工作。都是 PHP,gearman 只是某种消息代理。
猜你喜欢
  • 2011-10-08
  • 1970-01-01
  • 1970-01-01
  • 2017-01-08
  • 2011-01-25
  • 1970-01-01
  • 1970-01-01
  • 2017-02-14
  • 1970-01-01
相关资源
最近更新 更多