IDBTransaction

This article is in need of a technical review.

« IDBTransaction

The IDBTransaction interface of the IndexedDB API provides a static, asynchronous transaction on a database using event handler attributes. All reading and writing of data are done within transactions. You actually use IDBDatabase to start transactions and use IDBTransaction to set the mode of the transaction and access an object store and make your request. You can also use it to abort transactions.

Methods

Inherits from: EventTarget

IDBTransaction.abort
Returns immediately, and rolls back all the changes to objects in the database associated with this transaction. If this transaction has been aborted or completed, then this method throws an error event.
IDBTransaction.objectStore
Returns an IDBObjectStore representing an object store that is part of the scope of this transaction.

Properties

IDBTransaction.db Read only
The database connection with which this transaction is associated.
IDBTransaction.mode Read only
The mode for isolating access to data in the object stores that are in the scope of the transaction. For possible values, see Constants. The default value is readonly.
IDBTransaction.error Read only
The error returned in the event of an unsuccessful transaction. Null if the transaction is not finished, is finished and successfully committed, or was aborted with IDBTransaction.abort function. Returns the same DOMError as the request object which caused the transaction to be aborted due to a failed request, or a DOMError for the transaction failure not due to a failed request (such as QuotaExceededError or UnknownError).

Event handlers

IDBTransaction.onabort Read only
The event handler for the onabort event.
IDBTransaction.oncomplete Read only
The event handler for the oncomplete event.
IDBTransaction.onerror Read only
The event handler for the onerror event.

Mode constants

Obsolete since Gecko 25 (Firefox 25 / Thunderbird 25 / SeaMonkey 2.22)
This feature is obsolete. Although it may still work in some browsers, its use is discouraged since it could be removed at any time. Try to avoid using it.

These constants are no longer available. You should use the direct string constants instead. (bug 888598)

Transactions can have one of three modes:

Constant Value Description
READ_ONLY

"readonly"

(0 in Chrome)

Allows data to be read but not changed.

READ_WRITE

"readwrite"

(1 in Chrome)

Allows reading and writing of data in existing data stores to be changed.
VERSION_CHANGE

"versionchange"

(2 in Chrome)

Allows any operation to be performed, including ones that delete and create object stores and indexes. This mode is for updating the version number of transactions that were started using the setVersion() method of IDBDatabase objects. Transactions of this mode cannot run concurrently with other transactions.

Even if these constants are now deprecated, you can still use them to provide backward compatibility if required (in Chrome the change was made in version 21). You should code defensively in case the object is not available anymore:

var myIDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || { READ_WRITE: "readwrite" };

Example

In the following code snippet, we open a read/write transaction on our database and add some data to an object store. Note also the functions attached to transaction event handlers to report on the outcome of the transaction opening in the event of success or failure.

// Let us open our database
var request = window.indexedDB.open("toDoList", 4);
    
// store the result of opening the database in the db variable.
db = request.result;

// Create a new object ready for being inserted into the IDB
var newItem = [
  { taskTitle: "Walk dog", hours: 19, minutes: 30, day: 24, month: "December", year: 2013, notified: "no" }
];

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

// report on the success of opening the transaction
transaction.oncomplete = function(event) {
  note.innerHTML += '<li>Transaction opened for task addition.</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("toDoList");

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

request.onsuccess = function(event) {
  // report the success of our new item going into the database
  note.innerHTML += '<li>New item added to database.</li>';
};

Known browser issues

Older versions of Google Chrome serialize all transactions. So even if you have only read-only transactions and no read-write transaction, your transactions are executed one at a time. Any subsequent transactions are not executed until read-only transactions are completed. For the status, see bug 64076.

Specifications

Specification Status Comment
Indexed Database API Candidate Recommendation  

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 12-webkit
21
 
4.0 (2.0) 10 17 Not supported
Feature Android Firefox Mobile (Gecko) Firefox OS IE Phone Opera Mobile Safari Mobile
Basic support 4.4 6.0 (6.0) 1.0.1 10 17 Not supported

Be careful in Chrome as it still implements the old specification along the new one. Similarly it still has the prefixed webkitIndexedDB property even if the unprefixed indexedDB is present.

See also

To learn more about various topics, see the following

  • Starting transactions: IDBDatabase
  • Setting a range of keys: IDBKeyRange
  • Creating cursors: IDBIndex
  • The reference application for the examples in this reference: To-do Notifications (view example live.) Not every snippet appears in this example, but every example uses the same data structure and syntax, and they will make sense in the context of this example.

Document Tags and Contributors

Last updated by: chrisdavidmills,