jdbc - with-connection: What happened? -


in clojure project, changed dependency [org.clojure/java.jdbc "0.2.3"] [org.clojure/java.jdbc "0.3.3"]

i have received following error:

clojure.lang.compiler$compilerexception: java.lang.runtimeexception: no such var: sql/with-connection, compiling:(/volumes/hd2/env/prj/restore/src/restore/db.clj:80:5)

what happened? function deprecated?

background: needed execute! 0.2.3 didn't have it. 0.3.3 has it, lacks with-connection ?!?

please help.

with-connection not considered harmful reasons leonardoborges mentioned, makes working connection pools harder. decoupling function database access specific connections makes easier model. forcing queries use same connection should exception, not rule. clojure.core/java.jdbc 0.3.0 designed deprecate with-connection. accomodate that, whole api needed changed.

each database access function takes db spec parameter. if db-spec connection pool function executed on 1 of it's connections, otherwise implicit new connection made db-spec. database access functions result in connection each when connectionpools not used.

this means resultsets can no longer returned lazily. formerly, processing lazy sequences postponed while still inside with-connection block. need realized during function execution, or connection closed or new connection returned pool next access function.

so processing can done within scope of functions themselves, through 2 new named parameters: :row-fn , :result-set-fn. first transforms each row, second collection of rows. if :result-set-fn returns lazy sequence, connection or resultset closed exception when using later. default :result-set-fn doall. when using own, make sure realized.

so general case, access , connections decoupled. exception: needing functions use same connection.

the common of these transaction use, uses scope indicate beginning , end of transaction. old transaction provided scope. new with-db-transaction function takes binding of new var , dp-spec.

this var bound 1 specific connection pool, or when no connection pools used, newly created connection. db access functions used inside block should use var instead of db-spec parameter.

(def db {..})  (with-db-transaction   [c db]   (let [from 1111         2222         sum 10         saldo-from (query c ["select saldo account id=?" from]                           :row-fn :saldo                           :result-set-fn first)         saldo-to (query c ["select saldo account id=?" to]                         :row-fn :saldo                         :result-set-fn first)]     (update! c :account {:saldo (- saldo-from sum)} ["id=?" from])     (update! c :account {:saldo (+ saldo-to sum)} ["id=?" to]))) 

a begin transaction command issued @ beginning. access use same connection that's passed functions instead of through dynamic scoping magic. when no exceptions generated, commit given @ end of scope.

when 1 specific connection, no transaction mechanic needed, there's with-db-connection function has same semantics. if want execute command set session setting, , queries on connection, can following:

(def db {..})  (with-db-connection [c db]                     (execute! c ["alter session set nls_sort='italian'"])                     (query c ["select * person name=?" "mario"]                            :row-fn (comp concat (juxt :name :surname)))) 

connection pools have specific on-open , on-close commands part of specs. using connections pool have same session settings set, , with-db-connection not needed @ all.


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -