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
Exception | Description |
---|---|
InvalidStateError |
The 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();
Specification
Specification | Status | Comment |
---|---|---|
Indexed Database API Draft The definition of 'IDBTransaction.commit()' in that specification. |
Recommendation | Unitial definition. |
Browser compatibility
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.
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
commit | Chrome Full support 76 | Edge Full support 79 | Firefox Full support 74 | IE No support No | Opera Full support 63 | Safari No support No | WebView Android Full support 76 | Chrome Android Full support 76 | Firefox Android No support No | Opera Android Full support 54 | Safari iOS No support No | Samsung Internet Android Full support 12.0 |
Legend
- Full support
- Full support
- No support
- No support
See also
- Using IndexedDB
- Starting transactions:
IDBDatabase
- Using transactions:
IDBTransaction
- Setting a range of keys:
IDBKeyRange
- Retrieving and making changes to your data:
IDBObjectStore
- Using cursors:
IDBCursor
- Reference example: To-do Notifications (view example live.)