Transactions
Before we go any further, we need to cover how Web SQL Database handles transactions. You can execute SQL commands against the database without using a transaction, but it's not recommended. Using a transaction gives you the opportunity to rollback any data you added, deleted, or changed in case of an error. In this way, transactions help maintain the consistency of the database.
Note
The snippets in this section are located at snippets/08/ex2-transactions/
in the code package of this book. When using the interactive snippet playground, select 8: Web SQL Database and Example 2.
Transactions are easily initiated by calling transaction
on the database object, as follows:
db.transaction( transactionCallback, error, success )
Note
Although it may appear from the first glance that Web SQL Database is asynchronous due to the use of callbacks, this isn't really the case. IndexedDB is the only local storage solution that is truly asynchronous.
Technically, the last two parameters to transaction...