Reputation: 934
What will be the best way to implement multiple DB queries or any sync operation on a new thread?
Lets take for example the following code:
(let [res1 (mysql/query...)
res2 (mysql/query...)
res3 (mysql/query...)
final (do-something res1 res2 res3)]
(http/ok final))
In this example res1
res2
res3
are not connected to each other.. meaning that you can execute all of them at the same time and then wait for all.
I would like that the 3 queries will go on the same time and then wait for all of them togehter.
Upvotes: 1
Views: 189
Reputation: 3538
Probably the simplest way is to use Clojure's built-in thread pool with future objects.
(let [res1 (future (mysql/query...))
res2 (future (mysql/query...))
res3 (future (mysql/query...))
final (do-something @res1 @res2 @res3)]
(http/ok final))
However, you will need to use separate Connection objects in the new threads because JDBC connections are not meant to be shared.
Upvotes: 3