https://kotlinlang.org logo
Title
m

Matej Drobnič

07/01/2019, 5:02 PM
Is there SQL database that supports nonblocking access? It is kinda moot to use nonblocking ktor server only to end up blocking on JDBC queries.
v

Vlad

07/02/2019, 7:52 AM
MySQL 8 has non blocking way of executing queries with X DevAPI. The API is kinda bad with a lot of bugs, but with some additional code it is usable.
c

Cameron Hashemi

07/02/2019, 8:03 AM
you can use a dedicated thread pool for blocking JDBC queries, e.g.
withContext(<http://Dispatchers.IO|Dispatchers.IO>) {}
Details and a proof-of-concept in this article: https://ryanharrison.co.uk/2018/04/14/kotlin-ktor-exposed-starter.html
v

Vlad

07/02/2019, 8:14 AM
It is great that Exposed now has suspendedTransaction, before that for transaction management they were using thread-local variables, which was deal-breaker for me then 🙂
m

Matej Drobnič

07/02/2019, 10:28 AM
How can Exposed suspend transaction?
It still uses JDBC behind the scenes right?
So it has to block at some point
d

Dennis Schröder

07/02/2019, 10:29 AM
But ... sorry there is a but ... I used Exposed with the DAO Model and when dealing with lazy executing queries, the needed Transaction will not be found.
v

Vlad

07/02/2019, 11:38 AM
@Matej Drobnič I am not sure that I understand what you mean by that... The
suspendedTransaction
function just "propagates" the transaction by keeping it in the
CoroutineContext
, so it won't be "lost" when different thread resumes the execution.
m

Matej Drobnič

07/02/2019, 11:39 AM
you can use a dedicated thread pool for blocking JDBC queries, e.g.
Right, but how is this any better than just going blocking all the way if you are already going to use a lot of blocking threads?
because most web requests just hit database, so 90% of the time, request would be blocking
v

Vlad

07/02/2019, 2:27 PM
The dedicated thread pool just gives a way to limit the number of concurrent requests to the database.
If you have pool with DB connections and thread pool with appropriate number of threads, only those threads will block, the other threads are free to handle web requests.
o

obobo

07/02/2019, 6:54 PM
I've used jasync-db on postgres, works really well.