Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dedicated connection for long query #296

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions aa_composer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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\
Expand Down Expand Up @@ -1813,7 +1813,7 @@ function checkBalances() {
});
},
function () {
conn.release();
close();
unlock();
}
);
Expand Down
37 changes: 34 additions & 3 deletions sqlite_pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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;
Expand Down