IDBTransaction.commit() - Web APIs 编辑

The commit() method of the IDBTransaction interface commits the transaction if it is called on an active transaction.

Note that commit() doesn't normally have to be called — a transaction will automatically commit when all outstanding requests have been satisfied and no new requests have been made. commit() can be used to start the commit process without waiting for events from outstanding requests to be dispatched.

If it is called on a transaction that is not active, it throws an InvalidStateError DOMException.

Note:

This feature is available in Web Workers.

Syntax

transaction.commit();

Parameters

None.

Return value

Void.

Exceptions

ExceptionDescription
InvalidStateErrorThe transaction state is not active.

Examples

// open a read/write db transaction, ready for adding the data
var transaction = db.transaction(["myDB"], "readwrite");

// report on the success of opening the transaction
transaction.oncomplete = function(event) {
  note.innerHTML += '<li>Transaction completed: database modification finished.</li>';
};

transaction.onerror = function(event) {
  note.innerHTML += '<li>Transaction not opened due to error. Duplicate items not allowed.</li>';
};

// create an object store on the transaction
var objectStore = transaction.objectStore("myObjStore");

// add our newItem object to the object store
var objectStoreRequest = objectStore.add(newItem[0]);

objectStoreRequest.onsuccess = function(event) {
  // report the success of the request (this does not mean the item
  // has been stored successfully in the DB - for that you need transaction.onsuccess)
  note.innerHTML += '<li>Request successful.</li>';
};

// Force the changes to be committed to the database asap
transaction.commit();

Specifications

SpecificationStatusComment
Indexed Database API 2.0
The definition of 'IDBTransaction.commit()' in that specification.
RecommendationUnitial definition.

Browser compatibility

BCD tables only load in the browser

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:112 次

字数:4884

最后编辑:8 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文