@@ -116,5 +116,29 @@ SQLiteOPResult genericSqliteOpenDb(string const dbName, string const docPath,
116116 .errorMessage = sqlite3_errmsg (*db)};
117117 }
118118
119+ // Set journal mode directly when opening.
120+ // This may have some overhead on the main thread,
121+ // but prevents race conditions with multiple connections.
122+ if (sqlOpenFlags & SQLITE_OPEN_READONLY) {
123+ exit = sqlite3_exec (*db, " PRAGMA busy_timeout = 30000;"
124+ // Default to normal on all connections
125+ " PRAGMA synchronous = NORMAL;" ,
126+ nullptr , nullptr , nullptr
127+ );
128+ } else {
129+ exit = sqlite3_exec (*db, " PRAGMA busy_timeout = 30000;"
130+ " PRAGMA journal_mode = WAL;"
131+ // 6Mb 1.5x default checkpoint size
132+ " PRAGMA journal_size_limit = 6291456;"
133+ // Default to normal on all connections
134+ " PRAGMA synchronous = NORMAL;" ,
135+ nullptr , nullptr , nullptr
136+ );
137+ }
138+ if (exit != SQLITE_OK) {
139+ return SQLiteOPResult{.type = SQLiteError,
140+ .errorMessage = sqlite3_errmsg (*db)};
141+ }
142+
119143 return SQLiteOPResult{.type = SQLiteOk, .rowsAffected = 0 };
120144}
0 commit comments