4.9. The IDBTransaction interface
Transaction objects implement the following interface:
- [
Exposed =(Window ,Worker )]interface :
IDBTransaction EventTarget {readonly attribute DOMStringList objectStoreNames ;readonly attribute IDBTransactionMode mode ;readonly attribute IDBTransactionDurability durability ;- [
SameObject ]readonly attribute IDBDatabase db ;readonly attribute DOMException ?error ;IDBObjectStore objectStore (DOMString );
name void commit ();void abort ();- // Event handlers:
attribute EventHandler onabort ;attribute EventHandler oncomplete ;attribute EventHandler onerror ;- };
enum {
IDBTransactionMode ,
"readonly" ,
"readwrite"
"versionchange" - };
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 . [durability](#dom-idbtransaction-durability)
Returns the durability hint the transaction was created with (["strict"](#dom-idbtransactiondurability-strict)
, ["relaxed"](#dom-idbtransactiondurability-relaxed)
), or ["default"](#dom-idbtransactiondurability-default)
).
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://heycam.github.io/webidl/#idl-DOMException)
) providing the reason.
The objectStoreNames
attribute’s getter must run these steps:
If this is an upgrade transaction, then return a
[DOMStringList](https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#domstringlist)
associated with a sorted name list of the names of the object stores in this‘s connection‘s object store set.Otherwise, return a
[DOMStringList](https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#domstringlist)
associated with a sorted name list of the names of the object stores in this‘s scope.
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 this‘s mode.
The durability
attribute’s getter must return this‘s durability hint.
🚧 The [durability](#dom-idbtransaction-durability)
attribute is new in this edition. It is supported in Chrome 82 and Edge 82. 🚧
The db
attribute’s getter must return this‘s connection‘s associated database.
The error
attribute’s getter must return this‘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://heycam.github.io/webidl/#aborterror)
“ [DOMException](https://heycam.github.io/webidl/#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://heycam.github.io/webidl/#quotaexceedederror)
“, “[ConstraintError](https://heycam.github.io/webidl/#constrainterror)
“, or “[UnknownError](https://heycam.github.io/webidl/#unknownerror)
“ [DOMException](https://heycam.github.io/webidl/#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://heycam.github.io/webidl/#aborterror)
“ [DOMException](https://heycam.github.io/webidl/#idl-DOMException)
and all changes made to the database will be reverted.
transaction . [commit()](#dom-idbtransaction-commit)
Attempts to commit the transaction. All pending requests will be allowed to complete, but no new requests will be accepted. This can be used to force a transaction to quickly finish, without waiting for pending requests to fire success
events before attempting to commit normally.
The transaction will abort if a pending request fails, for example due to a constraint error. The success
events for successful requests will still fire, but throwing an exception in an event handler will not abort the transaction. Similarly, error
events for failed requests will still fire, but calling preventDefault()
will not prevent the transaction from aborting.
The objectStore(name)
method, when invoked, must run these steps:
If transaction’s state is finished, then throw an “
[InvalidStateError](https://heycam.github.io/webidl/#invalidstateerror)
“[DOMException](https://heycam.github.io/webidl/#idl-DOMException)
.Let store be the object store named name in this‘s scope, or throw a “
[NotFoundError](https://heycam.github.io/webidl/#notfounderror)
“[DOMException](https://heycam.github.io/webidl/#idl-DOMException)
if none.Return an object store handle associated with store and this.
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:
If this‘s state is committing or finished, then throw an “
[InvalidStateError](https://heycam.github.io/webidl/#invalidstateerror)
“[DOMException](https://heycam.github.io/webidl/#idl-DOMException)
.Set this‘s state to inactive and run abort a transaction with null as error.
The commit()
method, when invoked, must run these steps:
If this‘s state is not active, then throw an “
[InvalidStateError](https://heycam.github.io/webidl/#invalidstateerror)
“[DOMException](https://heycam.github.io/webidl/#idl-DOMException)
.Run commit a transaction with this.
🚧 The [commit()](#dom-idbtransaction-commit)
method is new in this edition. It is supported in Chrome 76, Edge 79, and Firefox 74.
🚧
It is not normally necessary to call [commit()](#dom-idbtransaction-commit)
on a transaction. A transaction will automatically commit when all outstanding requests have been satisfied and no new requests have been made. This call can be used to start the commit process without waiting for events from outstanding requests to be dispatched.
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.