如何在IndexedDB的一个事务中提出几个请求 [英] How can I put several requests in one transaction in IndexedDB

查看:110
本文介绍了如何在IndexedDB的一个事务中提出几个请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码如下:

...
var f1 = function(trans) {
  var store = trans.objectStore('ObjectStore');
  store.clear();
};
var f2 = function(trans) {
  var store = trans.objectStore('ObjectStore');
  store.add({id: 1, data: 'text'});
};
...
var trans = DB.connection.transaction(['ObjectStore'], IDBTransaction.READ_WRITE);
trans.onerror = function() { alert('ERROR'); };
trans.oncomplete = function() { alert('DONE'); };
...

出现问题,我在clear之后并在第二个请求异常时得到DONE警报.

The problem us that i get the DONE alert right after clear and on the second request exception appears.

是否可以重用" IndexedDB中的事务?

Is it possible to "reuse" the transaction in IndexedDB?

UPD:我发现上面的代码在我最新的Chromium夜间版本中可以正常工作.

UPD: I've found that the code above works as I expect in the latest Chromium nightly build.

推荐答案

根据Mozilla开发人员和IndexedDB的合作规范编写者Jonas Sicking 的说法,上一次回调触发时将提交事务.因此,要使事务保持活动状态,您应该能够通过连续的回调来重用它.

According to Jonas Sicking, a Mozilla dev and co-spec writer for IndexedDB, a transaction is committed when the last callback fires. So to keep a transaction alive, you should be able to reuse it via successive callbacks.

您使用的是上面的oncomplete,但是onsucess应该也能正常工作.

You're using oncomplete above, but onsucess should work equally as well.

您可以找到事务对象作为在回调中返回的请求对象的属性.

You can find the transaction object as an attribute of the request object that's returned on callback.

以下句子是不正确的:今天的交易会自动提交 当交易变量超出范围且没有更多请求时 可以放在上面".

The following sentence isn't correct "Transactions today auto-commit when the transaction variable goes out of scope and no more requests can be placed against it".

当变量用完时,事务永远不会自动提交 范围.通常,它们仅在上一次成功/错误回调时才提交 触发,并且该回调不调度更多请求.所以不是 与任何变量的范围有关.

Transaction never automatically commit when a variable goes out of scope. Generally they only commit when the last success/error callback fires and that callback schedules no more requests. So it's not related to the scope of any variables.

唯一的例外是如果您创建交易但未放置 反对它的要求.在这种情况下,交易是已提交"的 (这意味着对于没有请求的交易) 当您返回事件循环时.在这种情况下,您可以 从技术上讲,一旦对交易的所有引用都提交"该交易 超出范围,但这并不是一个特别有趣的用例 优化.

The only exception to this is if you create a transaction but place no requests against it. In that case the transaction is "committed" (whatever that means for a transaction which has no requests) as soon as you return to the event loop. In this scenario you could technically "commit" the transaction as soon as all references to it go out of scope, but it's not a particularly interesting use case to optimize.

基于下面的规范示例,您应该能够可以在evt.transaction处找到事务对象,然后可以进行新的事务并添加新的onsuccess事件侦听器.

Based on a spec example below, you should be able to find the transaction object at evt.transaction, and you can do a new transaction and add a new onsuccess event listener.

 var request = indexedDB.open('AddressBook', 'Address Book');
 request.onsuccess = function(evt) {...};
 request.onerror = function(evt) {...};

这篇关于如何在IndexedDB的一个事务中提出几个请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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