Repository navigation
Are the Queries run in coroutines? Or, do I need to call them under a coroutine? #537
Answered
by
JasonMing
meta-forte
asked this question in
Q&A
|
Does ktorm run SQL statements & queries by starting a coroutine? or, do I need manage execution of queries under coroutines myself? thanks |
Answered by
JasonMing
Aug 19, 2024
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 |
0 replies
Answer selected by
meta-forte
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to 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
JdbcTransactionmanagerdepends 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.