0

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.

tubu13
  • 924
  • 2
  • 17
  • 34

1 Answers1

2

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.

erdos
  • 3,135
  • 2
  • 16
  • 27