sqlite sets configurable busy_timeout, does immediate transactions
authorJustin Wind <justin.wind+git@gmail.com>
Fri, 14 Jun 2024 18:29:58 +0000 (11:29 -0700)
committerJustin Wind <justin.wind+git@gmail.com>
Sat, 13 Jul 2024 17:34:30 +0000 (10:34 -0700)
lib/sqlite-creator.js

index 43a2449c2aa72b2722ad6c6cee42b2206b9c292d..f1cc1710e5e3682de611e9512c7900cf3965fe37 100644 (file)
@@ -43,6 +43,7 @@ const SQLiteCreator = (Abstract) => {
      * @param {object} options.db db options
      * @param {string} options.db.connectionString db connection string
      * @param {string=} options.db.queryLogLevel db query log level
+     * @param {number=} options.db.sqliteBusyTimeoutMs busy timeout
      * @param {bigint=} options.db.sqliteOptimizeAfterChanges optimize db after this many changes
      */
     constructor(logger, options) {
@@ -65,6 +66,8 @@ const SQLiteCreator = (Abstract) => {
       this.optimizeAfterChanges = options.db.sqliteOptimizeAfterChanges || 0; // Default to no periodic optimization.
       this.db.pragma('foreign_keys = on'); // Enforce consistency.
       this.db.pragma('journal_mode = WAL'); // Be faster, expect local filesystem.
+      const busyTimeout = options.db.sqliteBusyTimeoutMs || 5000;
+      this.db.pragma(`busy_timeout = ${busyTimeout}`); // Don't immediately fall over when busy.
       this.db.defaultSafeIntegers(true); // This probably isn't necessary, but by using these BigInts we keep weird floats out of the query logs.
     }
 
@@ -93,7 +96,7 @@ const SQLiteCreator = (Abstract) => {
      * @returns {string} sql
      */
     _createMetaVersionTable() {
-      return this.db.exec(`BEGIN;
+      return this.db.exec(`BEGIN IMMEDIATE;
 CREATE TABLE _meta_schema_version (
   major INTEGER NOT NULL CHECK (typeof(major) = 'integer'),
   minor INTEGER NOT NULL CHECK (typeof(minor) = 'integer'),
@@ -279,7 +282,7 @@ COMMIT;`);
 
     transaction(dbCtx, fn) {
       dbCtx = dbCtx || this.db;
-      return dbCtx.transaction(fn)();
+      return dbCtx.transaction(fn).immediate();
     }