0

I'm trying to build an application using Ktor and Exposed with SQLite. Unfortunately my application keeps crashing.

This is my database connection:

class DatabaseFactory(connection: DatabaseConnection) {
    init {
        connect()
        createSchema()
    }

    private fun connect() {
        Database.connect("jdbc:sqlite:/path/to/file", "org.sqlite.JDBC")
        TransactionManager.manager.defaultIsolationLevel = Connection.TRANSACTION_SERIALIZABLE
    }

    private fun createSchema() {
        transaction {
            SchemaUtils.create(Images)
            SchemaUtils.create(KeyValues)

            Images.deleteAll()
        }
    }

    suspend fun <T> dbQuery(block: () -> T): T =
        withContext(Dispatchers.IO) {
            transaction { block() }
        }
}

// access db with
DatabaseFactory.dbQuery {
    // do stuff
}

While running my application I get the following error:

org.jetbrains.exposed.exceptions.ExposedSQLException: org.sqlite.SQLiteException: [SQLITE_BUSY]  The database file is locked (database is locked)
multiholle
  • 3,050
  • 8
  • 41
  • 60

1 Answers1

0

When you use coroutines and run your code in Dispatchers.IO your queries could be executed in separate threads what treated as multiple processes which only could read from SQLite instance.

More on SQLite docs

Tapac
  • 1,889
  • 1
  • 14
  • 18