Skip to content

Commit 943a8be

Browse files
Jeff BrownAndroid (Google) Code Review
authored andcommitted
Merge "Increase busy timeout." into jb-dev
2 parents f053e23 + 676c519 commit 943a8be

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

core/jni/android_database_SQLiteConnection.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,20 @@
4141

4242
namespace android {
4343

44+
/* Busy timeout in milliseconds.
45+
* If another connection (possibly in another process) has the database locked for
46+
* longer than this amount of time then SQLite will generate a SQLITE_BUSY error.
47+
* The SQLITE_BUSY error is then raised as a SQLiteDatabaseLockedException.
48+
*
49+
* In ordinary usage, busy timeouts are quite rare. Most databases only ever
50+
* have a single open connection at a time unless they are using WAL. When using
51+
* WAL, a timeout could occur if one connection is busy performing an auto-checkpoint
52+
* operation. The busy timeout needs to be long enough to tolerate slow I/O write
53+
* operations but not so long as to cause the application to hang indefinitely if
54+
* there is a problem acquiring a database lock.
55+
*/
56+
static const int BUSY_TIMEOUT_MS = 2500;
57+
4458
static struct {
4559
jfieldID name;
4660
jfieldID numArgs;
@@ -127,8 +141,8 @@ static jint nativeOpen(JNIEnv* env, jclass clazz, jstring pathStr, jint openFlag
127141
return 0;
128142
}
129143

130-
// Set the default busy handler to retry for 1000ms and then return SQLITE_BUSY
131-
err = sqlite3_busy_timeout(db, 1000 /* ms */);
144+
// Set the default busy handler to retry automatically before returning SQLITE_BUSY.
145+
err = sqlite3_busy_timeout(db, BUSY_TIMEOUT_MS);
132146
if (err != SQLITE_OK) {
133147
throw_sqlite3_exception(env, db, "Could not set busy timeout");
134148
sqlite3_close(db);

0 commit comments

Comments
 (0)