<< Back to previous view

[JDBC-58] get-connection returns an error Created: 24/May/13  Updated: 01/Jun/16  Resolved: 24/May/13

Status: Closed
Project: java.jdbc
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Defect Priority: Major
Reporter: Salil Wadnerkar Assignee: Sean Corfield
Resolution: Declined Votes: 0
Labels: None



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.

Comment by Sean Corfield [ 24/May/13 11:10 AM ]

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

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

Comment by Jason Wheeler [ 01/Sep/15 3:57 PM ]

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))

Comment by Sean Corfield [ 01/Sep/15 4:03 PM ]

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.

Generated at Sun Jan 21 15:06:49 CST 2018 using JIRA 4.4#649-r158309.