Skip to content

Commit 2f14ce5

Browse files
committed
Set a busy timeout during connection configuration in Room
Setting a busy timeout helps with slow IO or auto-checkpoint locks in a multiple connection database. The amount of time set is somewhat arbitrary. Its based off the 2500 ms that Android bindings already use and is meant to handle small table locks but not too big to make operations hang and apps to feel unresponsive. If a busy timeout is already configured then Room will not override it, sometimes a developer might want to set their own timeout if their database is used by multiple processes frequently and concurrently. Bug: 304295573 Test: BuilderTest Change-Id: I4447861b285700e6359faf6353fd8564a629263a
1 parent bc64555 commit 2f14ce5

4 files changed

Lines changed: 51 additions & 6 deletions

File tree

‎room/integration-tests/multiplatformtestapp/src/commonTest/kotlin/androidx/room/integration/multiplatformtestapp/test/BaseBuilderTest.kt‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ import androidx.room.RoomDatabase
2222
import androidx.room.useReaderConnection
2323
import androidx.room.useWriterConnection
2424
import androidx.sqlite.SQLiteConnection
25+
import androidx.sqlite.SQLiteDriver
26+
import androidx.sqlite.driver.bundled.BundledSQLiteDriver
27+
import androidx.sqlite.execSQL
2528
import kotlin.coroutines.EmptyCoroutineContext
2629
import kotlin.test.Test
2730
import kotlinx.coroutines.CompletableDeferred
@@ -233,4 +236,29 @@ abstract class BaseBuilderTest {
233236

234237
database.close()
235238
}
239+
240+
@Test
241+
fun setCustomBusyTimeout() = runTest {
242+
val customBusyTimeout = 20000
243+
val actualDriver = BundledSQLiteDriver()
244+
val driverWrapper =
245+
object : SQLiteDriver by actualDriver {
246+
override fun open(fileName: String): SQLiteConnection {
247+
return actualDriver.open(fileName).also { newConnection ->
248+
newConnection.execSQL("PRAGMA busy_timeout = $customBusyTimeout")
249+
}
250+
}
251+
}
252+
val database = getRoomDatabaseBuilder().setDriver(driverWrapper).build()
253+
val configuredBusyTimeout =
254+
database.useReaderConnection { connection ->
255+
connection.usePrepared("PRAGMA busy_timeout") {
256+
it.step()
257+
it.getLong(0)
258+
}
259+
}
260+
assertThat(configuredBusyTimeout).isEqualTo(customBusyTimeout)
261+
262+
database.close()
263+
}
236264
}

‎room/room-runtime/src/commonMain/kotlin/androidx/room/RoomConnectionManager.kt‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ abstract class BaseRoomConnectionManager {
116116
*/
117117
private fun configurationConnection(connection: SQLiteConnection) {
118118
configureSynchronousFlag(connection)
119+
configureBusyTimeout(connection)
119120
openDelegate.onOpen(connection)
120121
}
121122

@@ -139,6 +140,19 @@ abstract class BaseRoomConnectionManager {
139140
}
140141
}
141142

143+
private fun configureBusyTimeout(connection: SQLiteConnection) {
144+
// Set a busy timeout if no timeout is set to avoid SQLITE_BUSY during slow I/O or during
145+
// an auto-checkpoint.
146+
val currentBusyTimeout =
147+
connection.prepare("PRAGMA busy_timeout").use {
148+
it.step()
149+
it.getLong(0)
150+
}
151+
if (currentBusyTimeout < BUSY_TIMEOUT_MS) {
152+
connection.execSQL("PRAGMA busy_timeout = $BUSY_TIMEOUT_MS")
153+
}
154+
}
155+
142156
protected fun onCreate(connection: SQLiteConnection) {
143157
val isEmptyDatabase = hasEmptySchema(connection)
144158
openDelegate.createAllTables(connection)
@@ -309,4 +323,13 @@ abstract class BaseRoomConnectionManager {
309323
private fun invokeOpenCallback(connection: SQLiteConnection) {
310324
callbacks.forEach { it.onOpen(connection) }
311325
}
326+
327+
companion object {
328+
/*
329+
* Busy timeout amount. This wait time is relevant to same-process connections, if a
330+
* database is used across multiple processes, it is recommended that the developer sets a
331+
* higher timeout.
332+
*/
333+
const val BUSY_TIMEOUT_MS = 3000
334+
}
312335
}

‎sqlite/sqlite-framework/src/nativeMain/kotlin/androidx/sqlite/driver/NativeSQLiteDriver.kt‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ import sqlite3.sqlite3_threadsafe
3636
*
3737
* Usage of this driver expects that `libsqlite` can be found in the shared library path.
3838
*/
39-
// TODO:
40-
// (b/304295573) busy handler registering
4139
class NativeSQLiteDriver : SQLiteDriver {
4240

4341
/**

‎sqlite/sqlite-framework/src/nativeMain/kotlin/androidx/sqlite/driver/NativeSQLiteStatement.kt‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,6 @@ import sqlite3.sqlite3_finalize
5656
import sqlite3.sqlite3_reset
5757
import sqlite3.sqlite3_step
5858

59-
/**
60-
* TODO:
61-
* * (b/304295573) busy / locked handling
62-
*/
6359
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) // For actual typealias in unbundled
6460
class NativeSQLiteStatement(
6561
private val dbPointer: CPointer<sqlite3>,

0 commit comments

Comments
 (0)