In node-sqlite3
, if the db is currently in serialized mode, will the next statement wait before the callback of the previous statement finishes, or will the callback run at the same time as the next statement?
What's the best way to write a transaction using node-sqlite3
? I've thought about these two approaches, but I'm not sure which one is correct, or even if they're both wrong.
// NEXT DB STATEMENT WAITS FOR CALLBACK TO COMPLETE?
db.serialize(() => {
db.run('BEGIN');
// statement 1
db.run(
sql1,
params1,
(err) => {
if (err) {
console.error(err);
return db.serialize(db.run('ROLLBACK'));
}
}
);
// statement 2
db.run(
sql2,
params2,
(err) => {
if (err) {
console.error(err);
return db.serialize(db.run('ROLLBACK'));
}
return db.serialize(db.run('COMMIT));
}
);
});
// NEXT DB STATEMENT DOES NOT WAIT FOR CALLBACK TO COMPLETE?
db.serialize(() => {
db.run('BEGIN');
// statement 1
db.run(
sql1,
params1,
(err) => {
if (err) {
console.error(err);
return db.serialize(db.run('ROLLBACK'));
}
db.serialize(() => {
// statement 2
db.run(
sql2,
params2,
(err) => {
if (err) {
console.error(err);
return db.serialize(db.run('ROLLBACK'));
}
return db.serialize(db.run('COMMIT));
}
);
});
}
);
});