diff --git a/aa_composer.js b/aa_composer.js index 40c0c658..54d01dee 100644 --- a/aa_composer.js +++ b/aa_composer.js @@ -1726,11 +1726,11 @@ function checkStorageSizes() { function checkBalances() { mutex.lockOrSkip(['checkBalances'], function (unlock) { - db.takeConnectionFromPool(function (conn) { // block conection for the entire duration of the check + db.takeConnectionForLongQueries(function ({ conn, close }) { // block conection for the entire duration of the check conn.query("SELECT 1 FROM aa_triggers", function (rows) { if (rows.length > 0) { console.log("skipping checkBalances because there are unhandled triggers"); - conn.release(); + close(); return unlock(); } var sql_create_temp = "CREATE TEMPORARY TABLE aa_outputs_balances ( \n\ @@ -1813,7 +1813,7 @@ function checkBalances() { }); }, function () { - conn.release(); + close(); unlock(); } ); diff --git a/sqlite_pool.js b/sqlite_pool.js index 90b5cb38..582b5aa3 100644 --- a/sqlite_pool.js +++ b/sqlite_pool.js @@ -39,7 +39,7 @@ module.exports = function(db_name, MAX_CONNECTIONS, bReadOnly){ var arrConnections = []; var arrQueue = []; - function connect(handleConnection){ + function connect(handleConnection, forLongQuery){ console.log("opening new db connection"); var db = openDb(function(err){ if (err) @@ -72,6 +72,10 @@ module.exports = function(db_name, MAX_CONNECTIONS, bReadOnly){ start_ts: 0, release: function(){ + if (forLongQuery) { + console.error('Please use close from arguments. Release not supported in long query!') + return; + } //console.log("released connection"); this.bInUse = false; if (arrQueue.length === 0) @@ -166,8 +170,11 @@ module.exports = function(db_name, MAX_CONNECTIONS, bReadOnly){ dropTemporaryTable: dropTemporaryTable }; - setInterval(connection.printLongQuery.bind(connection), 60 * 1000); - arrConnections.push(connection); + + if (!forLongQuery) { + setInterval(connection.printLongQuery.bind(connection), 60 * 1000); + arrConnections.push(connection); + } } // accumulate array of functions for async.series() @@ -222,6 +229,29 @@ module.exports = function(db_name, MAX_CONNECTIONS, bReadOnly){ arrQueue.push(handleConnection); } + function takeConnectionForLongQueries(handleConnection){ + if (!handleConnection) + return new Promise(resolve => takeConnectionForLongQueries(resolve)); + + if (!bReady){ + console.log("takeConnectionForLongQueries will wait for ready"); + eventEmitter.once('ready', function(){ + console.log("db is now ready"); + takeConnectionForLongQueries(handleConnection); + }); + return; + } + + connect((connection) => { + handleConnection({ + conn: connection, + close: () => { + connection.db.close(); + } + }) + }, true); + } + function onDbReady(){ if (bCordova && !cordovaSqlite) cordovaSqlite = window.cordova.require('cordova-sqlite-plugin.SQLite'); @@ -328,6 +358,7 @@ module.exports = function(db_name, MAX_CONNECTIONS, bReadOnly){ pool.query = query; pool.addQuery = addQuery; pool.takeConnectionFromPool = takeConnectionFromPool; + pool.takeConnectionForLongQueries = takeConnectionForLongQueries; pool.getCountUsedConnections = getCountUsedConnections; pool.close = close; pool.escape = escape;