https://kotlinlang.org logo
#squarelibraries
Title
# squarelibraries
c

Colton Idle

10/11/2023, 6:12 PM
Every now and then I'll get this issue
Copy code
android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5 SQLITE_BUSY): , while compiling: PRAGMA journal_mode
the stacktrace has some mentions of Sentry (the crash reporting tool) and it also points me to this block of code. 1. Does it seem like sentry could be the culprit?
Copy code
at io.sentry.android.sqlite.SentrySupportSQLiteOpenHelper$sentryWritableDatabase$2.invoke(SentrySupportSQLiteOpenHelper.kt:40)
2. or is my code here somehow bad?
Copy code
suspend fun itemExists(id: String): Boolean {
    var result = false
    withContext(myIoDispatcher) {
      val foo = myQueries.selectById(id).executeAsOneOrNull()
      if (foo != null) {
        result = true
      }
    }
    return result
  }
According to the only google search result hit. This could be from reading and writing to the database at one time. That seems weird though. My other method (that adds like 100k records at one time) is written like this
Copy code
suspend fun deleteAllThenInsertAll(bufferedReader: BufferedReader) =
      withContext(myIoDispatcher) {
        myQueries.transaction {
          myQueries.deleteAll()
// read bufferedReader line by line and insert in the transaction
Could it be... my provider was missing Singleton annotation?
Copy code
@Provides
fun provideDelight(@ApplicationContext context: Context): DbService {
  val driver = AndroidSqliteDriver(The.Schema, context, "the.db")
  return DbService(driver)
}
d

Daniel Perez

10/12/2023, 2:08 AM
That's what I was thinking. I'm under the impression your DB should be a singleton in your app.
c

Colton Idle

10/12/2023, 5:12 PM
I thought it was already. so definitely a "me" problem. lol
p

Paul Woitaschek

10/13/2023, 5:11 AM
We have this on iOS, and it's definitely a singleton
m

Matt Nelson

10/14/2023, 8:32 AM
Don't think I'll ever go back to using the
AndroidSqliteDriver
and all that android glue being used under the hood (and needing
Context
). sqlite-jdbc is where it's at, and loads the latest version of
SQLite
instead of using whatever version of
SQLite
is on that device. Checkout https://github.com/toxicity-io/sqlite-mc Can simply pass
Key.Empty
and not even use the encryption if you don't want to.
2 Views