Fork me on GitHub
#sql
<
2021-01-21
>
John Conti19:01:35

Hey there. Using java.jdbc 0.7.11 Noticed that the with-db-connection [conn db-spec {:auto-commit? false}] will not change the state of a connection if one is returned from db-find-connection:

(defmacro with-db-connection
  "Evaluates body in the context of an active connection to the database.
  (with-db-connection [con-db db-spec opts]
    ... con-db ...)"
  [binding & body]
  `(let [db-spec# ~(second binding) opts# ~(or (second (rest binding)) {})]
     (if (db-find-connection db-spec#)
       (let [~(first binding) db-spec#]
         ~@body)
       (with-open [con# (get-connection db-spec# opts#)]
         (let [~(first binding) (add-connection db-spec# con#)]
           ~@body)))))

seancorfield20:01:07

@john493 It would lead to some very hard to find bugs if you could pass a connection into a function and it mutated that connection without you knowing...

John Conti21:01:43

My thought was that it needed the same kind of thing transaction* has. As it is now, if one says with-db-connection <some valid db-spec> {:auto-commit? false} is simply non-deterministic. If you pass a datasource, it works, a connection pool, it doesn’t. So the abstraction that says we are assigning characteristics to the resulting connection with the values in the map is misleading, because it doesn’t reliably do that. The other choice would be to remove the feature, than have it work for only some valid values of db-spec.

seancorfield21:01:22

clojure.java.jdbc is effectively deprecated now so it almost certainly won't be changed (and it certainly won't have features removed). next.jdbc has superseded it.

seancorfield21:01:05

(there is no "with connection" in next.jdbc -- you're expected to use with-open if you want to create a new connection and reuse it over several SQL operations)