4.9. The IDBTransaction interface

transaction objects implement the following interface:

  1. [Exposed=(Window,Worker)]
  2. interface IDBTransaction : EventTarget {
  3. readonly attribute DOMStringList objectStoreNames;
  4. readonly attribute IDBTransactionMode mode;
  5. [SameObject] readonly attribute IDBDatabase db;
  6. readonly attribute DOMException error;
  7.  
  8. IDBObjectStore objectStore(DOMString name);
  9. void abort();
  10.  
  11. // Event handlers:
  12. attribute EventHandler onabort;
  13. attribute EventHandler oncomplete;
  14. attribute EventHandler onerror;
  15. };
  16.  
  17. enum IDBTransactionMode {
  18. "readonly",
  19. "readwrite",
  20. "versionchange"
  21. };

transaction . [objectStoreNames](#dom-idbtransaction-objectstorenames)

Returns a list of the names of object stores in the transaction’s scope. For an upgrade transaction this is all object stores in the database.

transaction . [mode](#dom-idbtransaction-mode)

Returns the mode the transaction was created with (["readonly"](#dom-idbtransactionmode-readonly) or ["readwrite"](#dom-idbtransactionmode-readwrite)), or ["versionchange"](#dom-idbtransactionmode-versionchange) for an upgrade transaction.

transaction . [db](#dom-idbtransaction-db)

Returns the transaction’s connection.

transaction . [error](#dom-idbtransaction-error)

If the transaction was aborted, returns the error (a [DOMException](https://www.w3.org/TR/WebIDL-1/#idl-DOMException)) providing the reason.

The objectStoreNames attribute’s getter must run these steps:

  1. If this transaction is an upgrade transaction, return a [DOMStringList](https://www.w3.org/TR/html52/single-page.html#domstringlist) associated with a sorted name list of the names of the object stores in this transaction‘s connection‘s object store set.

  2. Otherwise, return a [DOMStringList](https://www.w3.org/TR/html52/single-page.html#domstringlist) associated with a sorted name list of the names of the object stores in this transaction‘s scope.

🚧 The [objectStoreNames](#dom-idbtransaction-objectstorenames) attribute is new in this edition. It is supported in Chrome 48, Firefox 44, and Safari 10.1. 🚧

The contents of each list returned by this attribute does not change, but subsequent calls to this attribute during an upgrade transaction can return lists with different contents as object stores are created and deleted.

The mode attribute’s getter must return the mode of the transaction.

The db attribute’s getter must return the database connection of which this transaction is a part.

The error attribute’s getter must return this transaction‘s error, or null if none.

If this transaction was aborted due to a failed request, this will be the same as the request‘s error. If this transaction was aborted due to an uncaught exception in an event handler, the error will be a “[AbortError](https://www.w3.org/TR/WebIDL-1/#aborterror)[DOMException](https://www.w3.org/TR/WebIDL-1/#idl-DOMException). If the transaction was aborted due to an error while committing, it will reflect the reason for the failure (e.g. “[QuotaExceededError](https://www.w3.org/TR/WebIDL-1/#quotaexceedederror)“, “[ConstraintError](https://www.w3.org/TR/WebIDL-1/#constrainterror)“, or “[UnknownError](https://www.w3.org/TR/WebIDL-1/#unknownerror)[DOMException](https://www.w3.org/TR/WebIDL-1/#idl-DOMException)).

transaction . [objectStore](#dom-idbtransaction-objectstore)(name)

Returns an [IDBObjectStore](#idbobjectstore) in the transaction‘s scope.

transaction . [abort()](#dom-idbtransaction-abort)

Aborts the transaction. All pending requests will fail with a “[AbortError](https://www.w3.org/TR/WebIDL-1/#aborterror)[DOMException](https://www.w3.org/TR/WebIDL-1/#idl-DOMException) and all changes made to the database will be reverted.

The objectStore(name) method, when invoked, must run these steps:

  1. If transaction has finished, throw an “[InvalidStateError](https://www.w3.org/TR/WebIDL-1/#invalidstateerror)[DOMException](https://www.w3.org/TR/WebIDL-1/#idl-DOMException).

  2. Let store be the object store named name in this transaction‘s scope, or throw a “[NotFoundError](https://www.w3.org/TR/WebIDL-1/#notfounderror)[DOMException](https://www.w3.org/TR/WebIDL-1/#idl-DOMException) if none.

  3. Return an object store handle associated with store and this transaction.

Each call to this method on the same [IDBTransaction](#idbtransaction) instance with the same name returns the same [IDBObjectStore](#idbobjectstore) instance.

The returned [IDBObjectStore](#idbobjectstore) instance is specific to this [IDBTransaction](#idbtransaction). If this method is called on a different [IDBTransaction](#idbtransaction), a different [IDBObjectStore](#idbobjectstore) instance is returned.

The abort() method, when invoked, must run these steps:

  1. If this transaction is finished, throw an “[InvalidStateError](https://www.w3.org/TR/WebIDL-1/#invalidstateerror)[DOMException](https://www.w3.org/TR/WebIDL-1/#idl-DOMException).

  2. Unset the transaction‘s active flag and run the steps to abort a transaction with null as error.

The onabort attribute is the event handler for the abort event.

The oncomplete attribute is the event handler for the complete event.

The onerror attribute is the event handler for the error event.

To determine if a transaction has completed successfully, listen to the transaction‘s complete event rather than the success event of a particular request, because the transaction can still fail after the success event fires.