Skip to content

Commit 8b3c175

Browse files
committed
Serialize demo SQL and block concurrent taps
1 parent 0b1a52e commit 8b3c175

3 files changed

Lines changed: 24 additions & 11 deletions

File tree

sentry-samples/sentry-samples-android/src/main/java/io/sentry/samples/android/sqlite/SQLiteActivity.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,8 @@ class SQLiteActivity : ComponentActivity() {
353353

354354
/** Run the variant's SQL statement inside a manual, scope-bound transaction. */
355355
private fun onTap(variant: DemoVariant) {
356+
if (dbOperationInFlight) return
357+
356358
sqlDetail = if (heavyWork) variant.displayInfo.sqlHeavy else variant.displayInfo.sql
357359
runTick++ // shimmer the detail box outline in the integration color
358360

@@ -376,6 +378,8 @@ class SQLiteActivity : ComponentActivity() {
376378
* `ui.load` transaction owns the spans.
377379
*/
378380
private fun onLongPress(variant: DemoVariant) {
381+
if (dbOperationInFlight) return
382+
379383
sqlDetail = if (heavyWork) variant.displayInfo.sqlHeavy else variant.displayInfo.sql
380384
latestResult = "Opened the auto-load screen — its ui.load transaction owns the db spans."
381385
startActivity(UiLoadActivity.intent(this, variant.demo, heavyWork))
@@ -598,7 +602,7 @@ class SQLiteActivity : ComponentActivity() {
598602
}
599603

600604
/** Closes + deletes every demo database file (via [SampleDatabases]), then re-warms them. */
601-
private fun resetDatabases(): String {
605+
private suspend fun resetDatabases(): String {
602606
val cleared = SampleDatabases.reset(applicationContext)
603607
return "Dropped tables: cleared $cleared database file(s)."
604608
}

sentry-samples/sentry-samples-android/src/main/java/io/sentry/samples/android/sqlite/SampleDatabases.kt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import io.sentry.sqlite.SentrySQLiteDriver
1919
import kotlinx.coroutines.CoroutineScope
2020
import kotlinx.coroutines.Dispatchers
2121
import kotlinx.coroutines.launch
22+
import kotlinx.coroutines.sync.Mutex
23+
import kotlinx.coroutines.sync.withLock
2224

2325
/**
2426
* Process-lifetime holder for the demo databases used by [SQLiteActivity].
@@ -40,9 +42,14 @@ import kotlinx.coroutines.launch
4042
*/
4143
object SampleDatabases {
4244

45+
private val sqlAccess = Mutex()
46+
4347
val driverDirectLock = Any()
4448
val openHelperDirectLock = Any()
4549

50+
/** Serializes demo SQL and [reset] so handles are never closed mid-statement. */
51+
suspend fun <T> withSqlAccess(block: suspend () -> T): T = sqlAccess.withLock { block() }
52+
4653
@Volatile private var driverConnection: SQLiteConnection? = null
4754
@Volatile private var driverRoom2Db: SampleRoom2Database? = null
4855
@Volatile private var driverRoom3Db: SampleRoom3Database? = null
@@ -170,9 +177,9 @@ object SampleDatabases {
170177

171178
/**
172179
* Closes the open handles, deletes every demo database file, then re-warms. Returns the number of
173-
* files cleared.
180+
* files cleared. Waits for any in-flight demo SQL (including [UiLoadActivity]) to finish first.
174181
*/
175-
fun reset(context: Context): Int {
182+
suspend fun reset(context: Context): Int = withSqlAccess {
176183
closeAll()
177184
val appContext = context.applicationContext
178185
val names =
@@ -186,7 +193,7 @@ object SampleDatabases {
186193
)
187194
val cleared = names.count { appContext.deleteDatabase(it) }
188195
warmUp(appContext)
189-
return cleared
196+
cleared
190197
}
191198

192199
private fun closeAll() {

sentry-samples/sentry-samples-android/src/main/java/io/sentry/samples/android/sqlite/SqlStatements.kt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,15 @@ object SqlStatements {
5959
Array(rowCount * 2) { i -> if (i % 2 == 0) "song ${i / 2}" else "artist ${i / 2}" }
6060

6161
suspend fun execute(context: Context, demo: SqlDemo, heavy: Boolean): String =
62-
when (demo) {
63-
SqlDemo.DRIVER_DIRECT -> driverDirect(context, heavy)
64-
SqlDemo.DRIVER_ROOM2 -> driverWithRoom2(context, heavy)
65-
SqlDemo.DRIVER_ROOM3 -> driverWithRoom3(context, heavy)
66-
SqlDemo.OPENHELPER_DIRECT -> openHelperDirect(context, heavy)
67-
SqlDemo.OPENHELPER_ROOM -> openHelperWithRoom(context, heavy)
68-
SqlDemo.OPENHELPER_SQLDELIGHT -> openHelperWithSqlDelight(context, heavy)
62+
SampleDatabases.withSqlAccess {
63+
when (demo) {
64+
SqlDemo.DRIVER_DIRECT -> driverDirect(context, heavy)
65+
SqlDemo.DRIVER_ROOM2 -> driverWithRoom2(context, heavy)
66+
SqlDemo.DRIVER_ROOM3 -> driverWithRoom3(context, heavy)
67+
SqlDemo.OPENHELPER_DIRECT -> openHelperDirect(context, heavy)
68+
SqlDemo.OPENHELPER_ROOM -> openHelperWithRoom(context, heavy)
69+
SqlDemo.OPENHELPER_SQLDELIGHT -> openHelperWithSqlDelight(context, heavy)
70+
}
6971
}
7072

7173
// --- 1. SentrySQLiteDriver, used directly -------------------------------------------------

0 commit comments

Comments
 (0)