Creating transactions

All IndexedDB operations occur within the context of a transaction; this ensures the integrity of database activities between various requests, such as those initiated by separate windows or worker threads. If a transaction completes normally, changes made during the transactions are automatically committed (saved). If a transaction is stopped, changes are rolled back (reversed).

Transaction Types

IndexedDB provides three types of transactions:

Term Description

READ_ONLY

Allows operations that do not modify data.

READ_WRITE

Allows data to be read and modified. Blocks other READ_WRITE and VERSION_CHANGE transactions.

VERSION_CHANGE

Allows all operations, but blocks all other transactions. Object stores and indexes can only be created in the context when this transaction is active.

 

Examples

Requests can only be processed when transactions are active. The following example shows how to declare a transaction and then read data from an object store:

  var oTx = db.transaction( "Objectstore1", IDBTransaction.READ_ONLY);
  oTx.oncomplete = handleTransactionComplete;
  oTx.onabort = handleTransactionAbort;
  oTx.onerror = handleTransactionError;  try {
    var objStore = otx.objectStore("ObjectStore1");    
    var req = objStore.get("AKeyValue");
    req.onsuccess = function( evt ) { 
       doSomethingWithValue( evt.target.result );
    }
    req.onerror = handleRequestFailure;
  }
  catch (ex) { 
    handleException( ex );

Transactions can also limit access to object stores within the transaction. For example, a READ_WRITE transaction prevents VERSION_CHANGE transactions. As a result, use transactions that are appropriate for your needs. For example, use READ_ONLY transactions to read object stores.

VERSION_CHANGE transactions allow you to create (or delete) object stores and indexes and are initiated using the setVersion method, as shown in the following example:

// In this example, showResults() is a function that displays messages to the user.
if ( oDB == null ) {
   showResults( "Can't update version; the database is not open." );
} else {
  var dbReq = oDB.setVersion( "1.0" );
  dbReq.onsuccess = createSchema;
  dbReq.onerror = function( evt ) {
     showResults ( "The transaction encountered an error: " + 
                    evt.message ); 
  }
  dbReq.onabort = function( evt ) {
    showResults ( "The transaction was canceled." ); 
  }
  }
}
function createSchema(evt) 
{
   var oTx = evt.target.result;
   oTx.oncomplete = function( evt ) {
      showResults( "Transaction complete." );
   }
   oTx.onabort = function( evt ) {
      showResults( "Transaction canceled." );
   }
   oTx.onerror = function( evt ) {
      showResults( "Transaction error: " + evt.message + "." );
   }
   var oObjStore = oDB.createObjectStore( "ObjectStore1" );
}

In this example, the onsuccess event of the setVersion request calls a function that creates object stores and indexes.

IE Test Drive: Cookbook demo

Asynchronous versus synchronous

Indexed Database API ("IndexedDB")

Internet Explorer 10 Guide for Developers