-
Does ktorm run SQL statements & queries by starting a coroutine? or, do I need manage execution of queries under coroutines myself? thanks |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
AFAIK, ktorm is running in blocking mode(see: org.ktorm.database.Database#executeExpression), the actually query will block the caller's thread. Which means, ktorm queries should be dispatched to a standalone blocking dispatcher. Especially the operations depend on the transaction, because Here is what I do: private val dbDispatcher = newFixedThreadPoolContext(30, "ktorm-db")
suspend fun <R> db(block: suspend Database.() -> R): R =
withContext(dbDispatcher) {
block(db)
} So you can simply call |
Beta Was this translation helpful? Give feedback.
AFAIK, ktorm is running in blocking mode(see: org.ktorm.database.Database#executeExpression), the actually query will block the caller's thread.
Which means, ktorm queries should be dispatched to a standalone blocking dispatcher. Especially the operations depend on the transaction, because
JdbcTransactionmanager
depends onThreadLocal
.Here is what I do:
So you can simply call
db { ... }
in any suspend function without worrying about blocking the non-blocking coroutine dispatchers.