Ktor, how to deal with jdbc blocking calls


#1

Hi guys,

I’ve got a library to use within ktor which does blocking operations via jdbc. Unfortunately it’s our core library and I cannot get rid of it or make it asynchronous quickly. So I’ll have to call this library and block. Is it still possible for me to still use ktor coroutine structure and maybe schedule the query jobs on a thread pool but then how to I wait for the result all asynchronously through coroutines?


#2

Sorry just a quick reply, I found this:

Am I on the right track?
The thing is, I am also waiting for a result, so I am not just running something blocking.


#3

Usually you just invoke blocking operation and it works fine. The problem arises then you have multiple blocking calls running simultaneously. They can exhaust CommonPool and block completely. I see three possibility here:

  1. If operations are fast and you are sure that they won’t block forever, you can call them inside coroutines.
  2. If operations are long, but you have only few of them, you can use custom CoroutineContext with increased number of parallel threads.
  3. If you have a large number of those blocking operations, you should just good old executor. If you want to evaluate the result, you can create a Channel and send results to it.