HTML and CSS Reference
In-Depth Information
tx.executeSql('INSERT INTO foo (id, text) VALUES
¬ (1, “foobar”)');
db.transaction(function (tx) {
tx.executeSql('DROP TABLE foo');
// known to fail - so should rollback the DROP statement
tx.executeSql('INSERT INTO foo (id, text) VALUES
¬ (1, “foobar”)');
}, function (error) {
// error.message is “no such table: foo”
alert('Rollback triggered, the table “foo” was never
¬ dropped due to: ' + error.message);
db.transaction(function (tx) {
tx.executeSql('SELECT * FROM foo', [], function (tx,
¬ results) {
alert('found ' + results.rows.length + ' row');
}, function (tx, error) {
// this will never execute
alert('something went wrong: ' + error.message);
The steps in the previous code are:
1. Start a transaction that creates the table foo and then
inserts a single row.
2. Start a transaction that drops the table foo and then
incorrectly tries to insert a new row in foo.
3. The transaction fails, and rolls back the statements
(that is, it's as if Step 2 never happened).
4. Start a transaction that selects all the rows from foo
and alerts the number of rows.
5. The SQL query succeeds and shows “found 1 row.”
Transactions are used to ensure that an atomic block of queries
executes and that if any part fails, it rolls back.
Search WWH ::

Custom Search