knex silently broken with sqlite3 and config.pool.max > 1
Environment
Knex version: 0.16.5 Database + version: node-sqlite3 version 4.0.6, sqlite3 version 3.26.0 OS: Fedora 29 Linux x86_64 Node version: nodejs-10.15.3-1.fc29.x86_64
Bug
If knex is configured with { client: "sqlite3", pool: { max: 10 } } then things fail in obscure ways
Steps to reproduce:
run the following program with npx ts-node example.ts
import * as fs from "fs";
import * as knex from "knex";
// tslint:disable: no-console
async function main() {
const config: knex.Config = {
client: "sqlite3",
connection: {
filename: "/tmp/db.sqlite3",
},
useNullAsDefault: true,
pool: {
min: 0,
max: 10,
},
};
fs.unlinkSync("/tmp/db.sqlite3");
const client = knex.default(config);
const SID_LENGTH = 128;
await client.schema.createTable("groups", (table) => {
table.increments("id").primary();
table.string("sid", SID_LENGTH).unique().defaultTo("S-1-0-0");
table.string("name", 64).unique();
table.index(["sid"], "group_sid");
table.index(["name"], "group_name");
});
const create_group = (n: number) => {
return client("groups").insert({ name: `group${n}`, sid: `S-1-0-0-${n}` });
};
const promises = [];
for (let i = 0; i < 10; ++i) {
promises.push(create_group(i));
}
const result = await Promise.all(promises);
const rows = await client("groups").select("name");
console.log(`rows: ${rows.length}`);
}
main()
.then(() => {
console.log("Program done");
process.exit(0);
})
.catch((err: any) => {
console.dir(err);
process.exit(1);
});It produces the following output:
{ [Error: SQLITE_BUSY: database is locked] errno: 5, code: 'SQLITE_BUSY' }If the config.pool.max is not set or is set to 1 the program works and produces
rows: 10
Program doneI struggled with this for some time until I found this nugget in the documentation:
This connection pool has a default setting of a min: 2, max: 10 for the MySQL and PG libraries, and a single connection for sqlite3 (due to issues with utilizing multiple connections on a single file)
If knex with sqlite3 is broken for config.pool.max > 1 the knex library should at the very least log a warning. Personally I think it would be better to fail with an exception.
Question: What is the situation with knex+sqlite and multiple connections? Is there any way to make it work?
Source: knex/knex