YeSQL with connection pool?

I am checking if YeSQL can help in my Clojure project, but I cannot find any example of YeSQL using the connection pool.

Does this mean that YeSQL creates a new connection for each statement?

I also found an example of how to use transactions using the clojure.java.jdbc / with-db transaction, but I believe that it is deprecated (I have not tried it yet).

Does this mean that YeSQL depends on clojure.java.jdbc for commit / rollback control? in this case, shouldn't I just use clojure.java.jdbc only, since YeSQL doesn't offer too much (except for the names of my queries and their externalization)?

early

+5
source share
2 answers

YeSQL does not handle joins or a pool of joins. You need to process it from the outside and provide a connection instance for the request function.

As you can see from the YeSQL example from README :

; Define a database connection spec. (This is standard clojure.java.jdbc.) (def db-spec {:classname "org.postgresql.Driver" :subprotocol "postgresql" :subname "//localhost:5432/demo" :user "me"}) ; Use it standalone. Note that the first argument is the db-spec. (users-by-country db-spec "GB") ;=> ({:count 58}) ; Use it in a clojure.java.jdbc transaction. (require '[clojure.java.jdbc :as jdbc]) (jdbc/with-db-transaction [connection db-spec] {:limeys (users-by-country connection "GB") :yanks (users-by-country connection "US")}) 

If you ask how to add connection pool processing, you can check out the example from the Clojure Cookbook .

Regarding transaction processing, the YeSQL documentation is not, but the source is pretty easy to understand:

 (defn- emit-query-fn "Emit function to run a query. - If the query name ends in `!` it will call `clojure.java.jdbc/execute!`, - If the query name ends in `<!` it will call `clojure.java.jdbc/insert!`, - otherwise `clojure.java.jdbc/query` will be used." [{:keys [name docstring statement]}] (let [split-query (split-at-parameters statement) {:keys [query-args display-args function-args]} (split-query->args split-query) jdbc-fn (cond (= [\< \!] (take-last 2 name)) `insert-handler (= \! (last name)) `execute-handler :else `jdbc/query)] `(def ~(fn-symbol (symbol name) docstring statement display-args) (fn [db# ~@function-args ] (~jdbc-fn db# (reassemble-query '~split-query ~query-args)))))) 

That way, it will simply generate a function that either calls clojure.java.jdbc/execute! , or clojure.java.jdbc/insert! generated request. You may need to refer to the documentation for these functions for more details.

+3
source

When executing transactions using YesQL, I end the YesQL calls in the clojure.java.jdbc / with-db-transation call and pass the generated connection information to the call to the YesQL function, which they will use instead of the standard connection without a transaction, if one is provided. For instance:

 ;; supply a name for the transaction connection, t-con, based on the existing db connection (jdbc/with-db-transaction [t-con db-spec] ;; this is the yesql call with a second map specifying the transaction connection (create-client-order<! {...} {:connection t-con})) 

All completed YesQL calls using the {:connection t-con} card will be part of the same transaction.

+2
source

All Articles