-
-
Notifications
You must be signed in to change notification settings - Fork 836
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.
});
Metadata
Metadata
Assignees
Labels
Type
Projects
Milestone
Relationships
Development
Select code repository
Activity
Pita commentedon Apr 30, 2011
Yes, that would be awesome +1
kkaefer commentedon May 1, 2011
@Pita: Please note that transactions are already possible with
node-sqlite3
, there's just no specific interface for it. Nothing stops you from runningdb.run("BEGIN"); /* some queries inside the transaction */ db.run("COMMIT")
. You only have to pay attention that no other code holding the database handle injects queries inside the transaction that shouldn't be there.Pita commentedon May 1, 2011
I know that it's possible. I'm already doing it like this. But it's a ugly way cause I have to build a huge sql string manually.
kkaefer commentedon May 1, 2011
You don't have to build a single string; you can just use the regular APIs between
db.run("BEGIN")
anddb.run("COMMIT")
miccolis commentedon Sep 15, 2011
I'd also really love to see this API implemented. I'm refactoring https://github.com/developmentseed/couch-sqlite to work with long lived connections and it would be great to use this, rather than a pool of one connection.
/cc @gundersen
kkaefer commentedon Sep 15, 2011
2 eggrolls.
ghost commentedon Jul 17, 2012
What's the best way to do this? Is it sufficient to put the transaction inside db.serialize? Will that block other non-db things like http server events?
Merge pull request #3 from jeromew/patch-1
JoshuaWise commentedon Feb 24, 2016
I'd like an answer to ghost's question, please. It's not clear in the docs.
koistya commentedon May 20, 2016
If this library is used with ES2015+
async/await
(viasqlite
), how can you make sure that some extraneous queries are not being injected into the transaction?db.run
calls into a transaction. #907bpasero commentedon Oct 5, 2018
I would like to know under which condition a ROLLBACK should be issues? It is not clear to me if this is done automatically by the engine. In other words, I would assume that upon any failure, the entire transaction is being rolled back to the state before. Otherwise, what would be the motivation to use a transaction in the first place?
Merge pull request TryGhost#3 from gristlabs/marshalling_merge
daniellockyer commentedon May 18, 2022
Clsoing in favor of #304