Skip to content

Commit f7b28c2

Browse files
author
hideki
committed
Applied Serialized mode instead of Multi-Thread mode to SQLite Connection mode.
https://www.sqlite.org/threadsafe.html Serialized. In serialized mode, SQLite can be safely used by multiple threads with no restriction. By applying Serialized mode, [SQLiteDatabaseCorruptException: database disk image is malformed](https://github.com/couchbase/couchbase-lite-android/issues/985) could be fixed.
1 parent fca8b84 commit f7b28c2

1 file changed

Lines changed: 9 additions & 8 deletions

File tree

jni/source/com_couchbase_lite_internal_database_sqlite_SQLiteConnection.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ JNIEXPORT jlong JNICALL Java_com_couchbase_lite_internal_database_sqlite_SQLiteC
7878
sqliteFlags = SQLITE_OPEN_READWRITE;
7979
}
8080

81+
// Serialized. In serialized mode, SQLite can be safely used by multiple threads with no restriction.
82+
// https://www.sqlite.org/threadsafe.html
83+
sqliteFlags |= SQLITE_OPEN_FULLMUTEX;
84+
8185
const char* pathCStr = env->GetStringUTFChars(pathStr, NULL);
8286
std::string path(pathCStr);
8387
env->ReleaseStringUTFChars(pathStr, pathCStr);
@@ -94,15 +98,12 @@ JNIEXPORT jlong JNICALL Java_com_couchbase_lite_internal_database_sqlite_SQLiteC
9498
return 0;
9599
}
96100

97-
// Once Couchbase Lite change the supported Android API version to 16 or higher,
98-
// we should remove following comment out.
99-
// https://github.com/couchbase/couchbase-lite-android/issues/792
100101
// Check that the database is really read/write when that is what we asked for.
101-
// if ((sqliteFlags & SQLITE_OPEN_READWRITE) && sqlite3_db_readonly(db, NULL)) {
102-
// throw_sqlite3_exception(env, db, "Could not open the database in read/write mode.");
103-
// sqlite3_close(db);
104-
// return 0;
105-
// }
102+
if ((sqliteFlags & SQLITE_OPEN_READWRITE) && sqlite3_db_readonly(db, NULL)) {
103+
throw_sqlite3_exception(env, db, "Could not open the database in read/write mode.");
104+
sqlite3_close(db);
105+
return 0;
106+
}
106107

107108
// Set the default busy handler to retry automatically before returning SQLITE_BUSY.
108109
err = sqlite3_busy_timeout(db, BUSY_TIMEOUT_MS);

0 commit comments

Comments
 (0)