Skip to content

Commit 2d03884

Browse files
committed
Set journal_mode on open to avoid concurrency issues.
1 parent f3bec8c commit 2d03884

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

cpp/ConnectionPool.cpp

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,6 @@ ConnectionPool::ConnectionPool(std::string dbName, std::string docPath,
2626
readConnections[i] = new ConnectionState(
2727
dbName, docPath, SQLITE_OPEN_READONLY | SQLITE_OPEN_FULLMUTEX);
2828
}
29-
30-
if (true == isConcurrencyEnabled) {
31-
// Write connection WAL setup
32-
writeConnection.queueWork([](sqlite3 *db) {
33-
sqliteExecuteLiteralWithDB(db, "PRAGMA journal_mode = WAL;");
34-
sqliteExecuteLiteralWithDB(
35-
db,
36-
"PRAGMA journal_size_limit = 6291456"); // 6Mb 1.5x default checkpoint
37-
// size
38-
// Default to normal on all connections
39-
sqliteExecuteLiteralWithDB(db, "PRAGMA synchronous = NORMAL;");
40-
});
41-
42-
// Read connections WAL setup
43-
for (int i = 0; i < this->maxReads; i++) {
44-
readConnections[i]->queueWork([](sqlite3 *db) {
45-
sqliteExecuteLiteralWithDB(db, "PRAGMA synchronous = NORMAL;");
46-
});
47-
}
48-
}
4929
};
5030

5131
ConnectionPool::~ConnectionPool() {

cpp/ConnectionState.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)