qantik qantik - 1 month ago 24
Node.js Question

Asynchronous HTTP-requests on sqlite database in Node.js

I'm currently trying to to test a little server with multiple asynchronous HTTP-requests, however my sqlite3 database seems to block simultaneous queries issuing

SQLITE_BUSY
.

db.exec(query, (err) => {
if (err !== null) {
callback('Insertion failed.');
} else {
callback(null);
});


Is there a way to circumvent this problem using the sqlite3 module?

Thanks in advance.

Answer

Could you just use async.queue?

var async = require('async');

var q = async.queue(function(task, callback) {
    db.exec(task.query, function(err) {
        callback('Insertion failed.');
    });
    callback();
}, 1);

q.drain = function() {
    console.log('queue is empty, ready for more query');
};

q.push({query: "Your SQL INSERT query"}, function(err) {
    console.error(err);
});