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 is done within transactions. You actually use IDBDatabase
to start transactions and IDBTransaction
to set the mode of the transaction (e.g. is it readonly or readwrite), and access an IDBObjectStore
to make a request. You can also use it to abort transactions.
Note that as of Firefox 40, IndexedDB transactions have relaxed durability guarantees to increase performance (see bug 1112702.) Previously in a readwrite
transaction IDBTransaction.oncomplete
was fired only when all data was guaranteed to have been flushed to disk. In Firefox 40+ the complete
event is fired after the OS has been told to write the data but potentially before that data has actually been flushed to disk. The complete
event may thus be delivered quicker than before, however, there exists a small chance that the entire transaction will be lost if the OS crashes or there is a loss of system power before the data is flushed to disk. Since such catastrophic events are rare most consumers should not need to concern themselves further.
If you must ensure durability for some reason (e.g. you're storing critical data that cannot be recomputed later) you can force a transaction to flush to disk before delivering the complete
event by creating a transaction using the experimental (non-standard) readwriteflush
mode (see IDBDatabase.transaction
.
Note that transactions are started when the transaction is created, not when the first request is placed; for example consider this:
var trans1 = db.transaction("foo", "readwrite"); var trans2 = db.transaction("foo", "readwrite"); trans2.put("2", "key"); trans1.put("1", "key");
After the code is executed the object store should contain the value "2", since trans2
should run after trans1
.
Inherits from: EventTarget
IDBTransaction.abort
IDBTransaction.objectStore
IDBObjectStore
object representing an object store that is part of the scope of this transaction.IDBTransaction.db
Read only
IDBTransaction.mode
Read only
readonly
.IDBTransaction.objectStoreNames
Read only
DOMStringList
of the names of IDBObjectStore
objects.IDBTransaction.error
Read only
null
if the transaction is not finished, is finished and successfully committed, or was aborted with IDBTransaction.abort
function.IDBTransaction.onabort
Read only
abort
event, fired when the transaction is aborted.IDBTransaction.oncomplete
Read only
complete
event, thrown when the transaction completes successfully.IDBTransaction.onerror
Read only
error
event, thrown when the transaction fails to complete. Deprecated since Gecko 13 (Firefox 13 / Thunderbird 13 / SeaMonkey 2.10)
This feature has been removed from the Web standards. Though some browsers may still support it, it is in the process of being dropped. Avoid using it and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.
These constants are no longer available — they were removed in Gecko 25. You should use the string constants directly 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. Transactions in this mode are known as "upgrade 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" };
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. For a full working example, see our To-do Notifications app (view example live.)
// Let us open our database var DBOpenRequest = window.indexedDB.open("toDoList", 4); DBOpenRequest.onsuccess = function(event) { note.innerHTML += '<li>Database initialised.</li>'; // store the result of opening the database in the db variable. This is used a lot below db = DBOpenRequest.result; // Run the addData() function to add the data to the database addData(); }; function addData() { // 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 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("toDoList"); // add our newItem object to the object store var objectStoreRequest = objectStore.add(newItem[0]); objectStoreRequest.onsuccess = function(event) { // report the success of our new item going into the database note.innerHTML += '<li>New item added to database.</li>'; }; };
Specification | Status | Comment |
---|---|---|
Indexed Database API The definition of 'IDBTransaction' in that specification. | Recommendation | Initial definition |
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|---|
Basic support | 23webkit 24[1] (unprefixed) | (Yes) | 10 moz 16.0 (16.0) | 10, partial | 15 | 7.1 |
Available in workers | (Yes) | ? | 37.0 (37.0) | ? | 35 | ? |
objectStoreNames property | 48.0 | ? | 50.0 (50.0) | No support | 35 | No support |
Feature | Android | Android Webview | Edge | Firefox Mobile (Gecko) | Firefox OS | IE Phone | Opera Mobile | Safari Mobile | Chrome for Android |
---|---|---|---|---|---|---|---|---|---|
Basic support | 4.4 | (Yes) | (Yes) | 22.0 (22.0) | 1.0.1 | 10 | 22 | 8 | (Yes) |
Available in workers | (Yes) | (Yes) | ? | 37.0 (37.0) | (Yes) | ? | 35 | ? | (Yes) |
objectStoreNames property | No support | 48.0 | ? | No support | No support | No support | 35 | No support | 48.0 |
IDBDatabase
IDBTransaction
IDBKeyRange
IDBObjectStore
IDBCursor
© 2005–2017 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/IDBTransaction