【问题标题】:SQLite Success Callback Fires Before Transaction FinishesSQLite 成功回调在事务完成之前触发
【发布时间】:2016-01-30 06:23:16
【问题描述】:

在我的 ASP.NET Web 表单应用程序中,我需要执行一些 JQuery 操作来收集表单数据,然后遍历表单数据以保存到 SQLite 数据库,并在成功完成后关闭当前窗口。如果窗口保持打开状态,保存操作可以完美运行,但是一旦我将 window.close 添加到我的 SQLite 事务中的成功回调,窗口似乎在保存操作有时间完成之前关闭。

function save(closeWindow)
{
  //Gathering form data and creating the checkListInsert string containing my bulk insert statement  (i.e. Insert Into Table Select .... Union Select...)

  db.transaction(function (tx) {

                tx.executeSql(checkListInsert.substring(0, checkListInsert.length - 6), [], function (tx, result) {
                    if (closeWindow == "Yes") {
                        //window.close();  If left uncommented the data will not save.  When left commented the save occurs but of course the window is still open
                    }
                }, onSQLError);
        });
}

编辑:如果我将 window.close() 放在 window.setTimeout() 内 1 秒,一切正常。所以这绝对是一个时间问题。似乎在事务完成之前回调不应该触发?!

【问题讨论】:

  • 我们需要看看executeSql里面的代码。我会假设这是 Ajax 隐藏的地方,乍一看,它似乎没有正确地将您的关闭函数作为回调运行。
  • 我不确定您要查找的代码。 executeSql 就在那里。
  • 其中有一个对 executeSql 的调用。函数里面的代码不是。
  • 所有代码都在那里。 checkListInsert 是我的 SQL 插入语句,后跟 [] 因为没有参数,然后是我的成功回调,其中包括 if 语句,然后是我的 onSQLError 回调。
  • 是的,函数调用和参数列表都在那里。我们需要看看executeSql的inside之后会发生什么。或者至少我觉得 executeSql 是某种 3rd 方库的一部分。是吗?如果有,是什么?

标签: javascript asp.net sqlite web-sql


【解决方案1】:

使用表单中的行数作为预期插入的行数。比较一下 通过结果到rowsAffected来验证交易是否完成:

if (closeWindow == "Yes") 
  {
  if (result.rowsAffected === /*Number of rows inserted*/)
    {
    window.close();
    }
  }

【讨论】:

    【解决方案2】:

    你应该等待事务完成,而不是executeSql:

    function save(closeWindow) {
        db.transaction(function (tx) {
            tx.executeSql(checkListInsert.substring(0, checkListInsert.length - 6), []);
        }, function () { }, function () {
            if (closeWindow == "Yes") {
                window.close();
            }
        });
    }
    

    【讨论】:

      猜你喜欢
      • 2016-11-20
      • 1970-01-01
      • 2014-12-16
      • 2021-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多