-
-
Notifications
You must be signed in to change notification settings - Fork 841
Closed
Labels
Description
// Flat interface.
var trx = db.begin();
trx.on('commit', function() {});
trx.on('rollback', function() {});
trx.rollback();
trx.commit(); // No implicit commit when calling db.begin();
// Creates a new stack
db.transaction(function(trx) {
trx == this
// implicit begin
// transaction objects are like database objects:
trx.run("CREATE TABLE foo (id INT PRIMARY KEY, bar)");
trx.run("INSERT INTO foo VALUES(?, ?)", 1, "first text");
trx.run("INSERT INTO foo VALUES(?, ?)", 1, "second text"); // will fail
trx.run("INSERT INTO foo VALUES(?, ?)", 1, "third text", function(err) {
if (err) {
// Do nothing and the error will be ignored
// See below for more explanation
trx.clear();
trx.rollback();
trx.commit();
}
});
trx.on('rollback', function() {
// This is emitted after the transaction was rolled back.
});
trx.on('commit', function() {
// This is emitted after the transaction was committed successfully.
});
trx.on('error', function() {
// This will be called when a statement execution doesn't have a callback
// and no error handlers.
// removes all further statements from the stack. You can now add more
// statements to the stack. the automatic commit on empty stack persists.
// Does not remove the error handler, you have to do that manually.
trx.clear();
// removes all further statements from the stack and does a rollback of the
// entire transaction.
trx.rollback();
// removes all further statements from the stack and commits the transaction
trx.commit();
});
// when no error handler is attached and an error bubbles up to the transaction's
// error event, the transaction is rolled back and the rollback event is emitted.
// implicit commit when the stack is empty.
});