simplification of transaction handling

This commit is contained in:
zadam 2020-06-21 13:15:36 +02:00
parent 56d6384ae1
commit f0acfaf147

View file

@ -75,7 +75,10 @@ function stmt(sql) {
} }
function beginTransaction() { function beginTransaction() {
return stmt("BEGIN").run(); // DEFERRED means that the transaction does not actually start until the database is first accessed.
// Internally, the BEGIN DEFERRED statement merely sets a flag on the database connection that turns off
// the automatic commit that would normally occur when the last statement finishes.
return stmt("BEGIN DEFERRED").run();
} }
function commit() { function commit() {
@ -173,8 +176,6 @@ function getColumn(query, params = []) {
} }
function execute(query, params = []) { function execute(query, params = []) {
startTransactionIfNecessary();
return wrap(query, s => s.run(params)); return wrap(query, s => s.run(params));
} }
@ -183,14 +184,10 @@ function executeWithoutTransaction(query, params = []) {
} }
function executeMany(query, params) { function executeMany(query, params) {
startTransactionIfNecessary();
getManyRows(query, params); getManyRows(query, params);
} }
function executeScript(query) { function executeScript(query) {
startTransactionIfNecessary();
return dbConnection.exec(query); return dbConnection.exec(query);
} }
@ -213,31 +210,20 @@ function wrap(query, func) {
return result; return result;
} }
function startTransactionIfNecessary() {
if (!cls.get('isTransactional') || dbConnection.inTransaction) {
return;
}
beginTransaction();
}
function transactional(func) { function transactional(func) {
// if the CLS is already transactional then the whole transaction is handled by higher level transactional() call if (dbConnection.inTransaction) {
if (cls.get('isTransactional')) {
return func(); return func();
} }
cls.set('isTransactional', true); // this signals that transaction will be needed if there's a write operation
try { try {
beginTransaction();
const ret = func(); const ret = func();
if (dbConnection.inTransaction) { commit();
commit();
// note that sync rows sent from this action will be sent again by scheduled periodic ping // note that sync rows sent from this action will be sent again by scheduled periodic ping
require('./ws.js').sendPingToAllClients(); require('./ws.js').sendPingToAllClients();
}
return ret; return ret;
} }
@ -248,9 +234,6 @@ function transactional(func) {
throw e; throw e;
} }
finally {
cls.namespace.set('isTransactional', false);
}
} }
module.exports = { module.exports = {