java.jdbc

get-connection returns an error

Details

  • Type: Defect Defect
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Declined
  • Affects Version/s: None
  • Fix Version/s: None
  • Component/s: None
  • Labels:
    None
  • Environment:
    PostgreSQL

Description

My db-spec is:
(def db-spec {:classname "org.postgresql.Driver"
:subprotocol "postgresql"
:subname "sd_ventures_development"
:user "sd_ventures"
:password ""})

When I query using the old API:
(defn get-devices []
(jdbc/with-connection db-spec
(jdbc/with-query-results rs ["SELECT * FROM devices"]
(doall rs))))

it works and I get the list of devices.

With the new API -
(defn get-devices []
(let [conn (jdbc/get-connection db-spec)]
(jdbc/query conn ["SELECT * FROM devices"])))
it returns an error:
Exception: java.lang.IllegalArgumentException: db-spec org.postgresql.jdbc4.Jdbc4Connection@4c54daa9 is missing a required parameter
jdbc.clj:221 clojure.java.jdbc/get-connection
jdbc.clj:628 clojure.java.jdbc/db-with-query-results*

I am completely bowled over by this error. I checked the source code. It should never reach that statement in get-connection because I am supplying both subname and subprotocol.

Activity

Hide
Sean Corfield added a comment -

Not a bug. You are using the API incorrectly. query takes a db-spec NOT a connection:

(jdbc/query db-spec ["SELECT * FROM devices"])

Show
Sean Corfield added a comment - Not a bug. You are using the API incorrectly. query takes a db-spec NOT a connection: (jdbc/query db-spec ["SELECT * FROM devices"])
Sean Corfield made changes -
Field Original Value New Value
Status Open [ 1 ] Resolved [ 5 ]
Resolution Declined [ 2 ]
Hide
Jason Wheeler added a comment -

To accomplish what he is trying to accomplish, it looks like you must put the connection in a map with key :connection and then manually close it like so (exception handling omitted for brevity):

(def conn {:connection (jdbc/get-connection db-spec)})

(def results (jdbc/query conn ["SELECT * FROM devices"]))

(.close (:connection conn))

Show
Jason Wheeler added a comment - To accomplish what he is trying to accomplish, it looks like you must put the connection in a map with key :connection and then manually close it like so (exception handling omitted for brevity): (def conn {:connection (jdbc/get-connection db-spec)}) (def results (jdbc/query conn ["SELECT * FROM devices"])) (.close (:connection conn))
Hide
Sean Corfield added a comment -

Or do what the documentation says: http://clojure-doc.org/articles/ecosystem/java_jdbc/home.html#reusing-connections

i.e., use with-db-connection which manages the open/close of the connection across multiple DB operations.

Show
Sean Corfield added a comment - Or do what the documentation says: http://clojure-doc.org/articles/ecosystem/java_jdbc/home.html#reusing-connections i.e., use with-db-connection which manages the open/close of the connection across multiple DB operations.
Sean Corfield made changes -
Status Resolved [ 5 ] Closed [ 6 ]

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: