Search code examples
sqlitekotlinkotlin-exposed

Increase performance of kotlin SQLite using PRAGMA commands


I try to create and connect to SQLite database in Kotlin using jetbrains.exposed library.

import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.SchemaUtils
import org.jetbrains.exposed.sql.transactions.TransactionManager
import org.jetbrains.exposed.sql.transactions.transaction

private fun provideSqliteDatabase(databaseConfig: DatabaseConfig?): Database {
    val path = databaseConfig?.url ?: "./"
    File(path).mkdirs()

    logger.info("SQLite Database path: $path/database.db")
    return Database.connect("jdbc:sqlite:$path/database.db", "org.sqlite.JDBC")
        .apply {
            TransactionManager.manager.defaultIsolationLevel =
                Connection.TRANSACTION_READ_UNCOMMITTED
            provideTables()

        }
}

I try to increase the performance of my database using some PRAGMA SQLite commands. How can I set these configurations such as set JOURNAL_MODE:

PRAGMA JOURNAL_MODE='OFF';

Point: I run my program on Ubuntu 20.04.


Solution

  • You can try to extend your connection url with journal_mode=OFF. If you'll check SQLite driver tests you will find that it parses and executes such parameters automatically.