Fork me on GitHub

;; this works
(defn slack-allowed []
   (let [ds (jdbc/get-datasource datasource)]
     (with-open [con (jdbc/get-connection ds)]
       (conj '()
             (jdbc/execute! con ["SELECT ?" 7])
             (jdbc/execute! con ["SELECT ?" 8])))))

;; this doesn't - execute() is called on closed connection
(defn slack-denied []
   (let [ds (jdbc/get-datasource datasource)]
     (with-open [con (jdbc/get-connection ds)]
       (map (fn [num] (jdbc/execute! con ["SELECT ?" num])) (range 7 9)))))


map is lazy my friend


so the function is evaluated after the with-open has returned. (probably when the result is printed at the repl)


I can call execute! multiple times in a with-open but not using map - any ideas?


Forgot to mention the above is using next.jdbc


try using mapv


or wrap in a (doall


@dharrigan thanks .. that's it - and the repl trying to print forces the execution after the con is closed


you're welcome (also to danstone for further clarification!) 🙂

Joe Lane17:10:49

I'm returning 1mil records from a next.jdbc/execute! call. Is there a way to avoid realizing the entire resultset into memory? I looked into next.jdbc/plan but couldn't discern if that would help in this situation.


@lanejo01 plan is designed for that situation, yes.


Because it lets you process (`reduce`) the entire result set without realizing it into Clojure data.

Joe Lane18:10:44

Fantastic @seancorfield! My scenario is put all results on a channel one at a time. Does it make sense to just a/put! inside a reduce call?


Yeah, although bear in mind: a) you'll need to realize each row into Clojure data to put them on a channel and b) you'll still end up with all that data realized into memory depending on how much is buffered and how fast it gets processed out of the channel on the other end.


with put!, only the global backpressure on put! itself will keep all results from being in memory


(and I suspect folks may advise against coupling blocking I/O with core.async... and there it is)

😄 4

if you use >!! instead, the realization will have backpressure based on the channel buffer and consumption this should be outside a go block NB

Joe Lane18:10:23

Thanks guys