https://kotlinlang.org logo
#exposed
Title
# exposed
b

bitkid

07/09/2019, 9:44 PM
Copy code
Database.connect(datasource = HikariDataSource(poolConfig),
            manager = { ThreadLocalTransactionManager(it, Connection.TRANSACTION_REPEATABLE_READ, 0) })
what i am doing (which i have no idea if it's sane) is the follwing. i have a small helper method which moves the transaction the IO Dispatcher
Copy code
suspend fun <T> dbTransaction(database: Database, block: () -> T): TransactionResult<T> =
    suspendedTransactionAsync(<http://Dispatchers.IO|Dispatchers.IO>, database, false) {
        block()
    }
and in my route handlers look like this
Copy code
fun Route.logIn(database: Database, sessionHandler: SessionHandler) {
    post<Login> {
        val response = dbTransaction(database) {
            sessionHandler.login(data)
        }.await()
        call.respond(response)
    }
}
so i can either wait for the db stuff to return with await() or i don't (fire and forget)
i am wondering if it actually makes sense to use Dispatchers.IO .. as it will most probably use the current thread from DefaultDispatcher
@tapac this is how i am using it now with ktor
btw my code fails when i pass true to suspendedTransactionAsync .. with a connection closed exception
t

tapac

07/11/2019, 4:25 PM
@bitkid Could you please share a little sample which I could run to test your case?
5 Views