Ktor and postgres - batch insert not working

116 Views Asked by At

I have a ktor application, and I'm connecting to postgres DB and using pgJDBC and HikariDataSource, and using exposed ORM. I'm trying to do batch inserts for better performance. However, my inserts keep running as separate insert statements.

As per documentation in exposed (https://github.com/JetBrains/Exposed/wiki/DSL#batch-insert)

NOTE: The batchInsert function will still create multiple INSERT statements when interacting with your database. You most likely want to couple this with the rewriteBatchedInserts=true (or rewriteBatchedStatements=true) option of your relevant JDBC driver, which will convert those into a single bulkInsert. You can find the documentation for this option for MySQL here and PostgreSQL here.

I checked the postgres documentation for jdbc https://jdbc.postgresql.org/documentation/use/

and I added this flag like this

private fun createHikariDataSource(
        url: String,
        driver: String,
        user: String,
        password: String,
        maxPoolSize: Int,
        autoCommit: Boolean
    ) = HikariDataSource(HikariConfig().apply {
        addDataSourceProperty("reWriteBatchedInserts", true)
        driverClassName = driver
        jdbcUrl = url
        username = user
        this.password = password
        maximumPoolSize = maxPoolSize
        isAutoCommit = autoCommit
        isReadOnly = false
        transactionIsolation = "TRANSACTION_REPEATABLE_READ"
        addDataSourceProperty("reWriteBatchedInserts", true)
        validate()
    })

and tried to pass the flag in the url like

val connectionPool = createHikariDataSource(
            url = "$jdbcURL/$defaultDatabase?reWriteBatchedInserts=true",
            driver = driverClassName,
            user = username,
            password = password,
            maxPoolSize.toInt(),
            autoCommit.toBoolean()
        )

and still the batch statement won't work

0

There are 0 best solutions below