Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting node-sqlite3 SQLITE_BUSY

Tags:

node.js

sqlite

I have a problem with a webserver that stores data periodically in a node-sqlite3 driven database. It will work fine a few times and then just fails with

SQLITE_BUSY: database is locked

Sometimes all inserts are not completed to the database even when not failing, leaving only a portion of the inserts in the table.

setInterval(doInserts,60000);

doInserts() {
calculateData(function(data){
    if(data)
    {
        insertData(data);
    }
});
}

function insertData(data) {
var sqlite3 = require("sqlite3").verbose();

//user fs existSync to check if file exists and openSync(file,'w') to create if not

db.serialize(function(){
    var stmt = db.prepare("INSERT INTO mytable (col1,col2) values (?,?)");

    for(var i =0;i<data.length;i++)
        stmt.run(data[0],data[1]);
    stmt.finalize();
});
db.close();
}

Taking account I am new to both node and SQLite, I'm guessing that I am using something in the wrong way, causing multiple open threads or processes to lock up the db, but no documentation or research has gotten me any closer to a solution.

like image 881
Vort3x Avatar asked Feb 27 '14 06:02

Vort3x


People also ask

What is node SQLite?

This is a node. js driver (Asynchronous, non-blocking SQLite3 bindings) for sqlite3. It is written in JavaScript, does not require compiling. It provides all most all connection/query from SQLit3. This is probably one of the best modules used for working with SQLite3 database and the module is actively maintained.

What is sqlite3 used for?

A standalone command-line shell program called sqlite3 is provided in SQLite's distribution. It can be used to create a database, define tables, insert and change rows, run queries and manage an SQLite database file. It also serves as an example for writing applications that use the SQLite library.

How much traffic can SQLite handle?

The amount of web traffic that SQLite can handle depends on how heavily the website uses its database. Generally speaking, any site that gets fewer than 100K hits/day should work fine with SQLite. The 100K hits/day figure is a conservative estimate, not a hard upper bound.


2 Answers

Just chipping in what solved it for me. This was happening whenever I had a data viewer application looking at the database. Once closed, it began working again!

like image 119
Alex Jones Avatar answered Sep 24 '22 14:09

Alex Jones


I suspect you understand Node\Sqlite correctly. I had the same problem using Sqlite3#serialize. I ended up not using it in the end, and instead used the pattern in chapter 7.2.2 of Mixu's Node Book. to control the flow of the db queries. Basically you have an array of functions. Each function has a callback that invoke the next function.

like image 41
Hill5Air Avatar answered Sep 24 '22 14:09

Hill5Air