java.jdbc

Connections created form db-strings limit query and execute!

Details

  • Type: Enhancement Enhancement
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Declined
  • Affects Version/s: None
  • Fix Version/s: None
  • Component/s: None
  • Labels:
    None
  • Environment:
    [org.clojure/clojure "1.9.0"]
    [org.clojure/java.jdbc "0.7.3"]
    [com.h2database/h2 "1.4.197"]
    [com.microsoft.sqlserver/mssql-jdbc "6.3.6.jre8-preview"]

Description

The usage of db-strings wrapped as a connection seem to be inconsistent:

Calling query with a db-string works:

(let [db-string "jdbc:h2:mem:test"]
(jdbc/query db-string "SELECT 1"))
=> ({:1 1})

With a connection based on a db-string, it does not:
(let [conn (jdbc/get-connection "jdbc:h2:mem:test")]
(jdbc/query conn "SELECT 1"))
IllegalArgumentException db-spec conn10: url=jdbc:h2:mem:test user= is missing a required parameter clojure.java.jdbc/get-connection (jdbc.clj:379)

However, a connection based on a db-string and a prepared-statement does work:
(let [conn (jdbc/get-connection "jdbc:h2:mem:test")]
(jdbc/query conn (jdbc/prepare-statement conn "SELECT 1")))
=> ({:1 1})

Calling execute! does not seem to work with a prepared statement.

Works with db-string directly:
(clojure.java.jdbc/execute! "jdbc:h2:mem:test" "CREATE SCHEMA foo")
=> [0]

... but doesn't with a connection (same problem as with query):
(clojure.java.jdbc/execute! (clojure.java.jdbc/get-connection "jdbc:h2:mem:test") "CREATE SCHEMA foo")
IllegalArgumentException db-spec conn58: url=jdbc:h2:mem:test user= is missing a required parameter clojure.java.jdbc/get-connection (jdbc.clj:379)

... and does not even work with a prepared statement:
(let [db-string "jdbc:h2:mem:test"
conn (clojure.java.jdbc/get-connection db-string)]
(clojure.java.jdbc/execute! conn
(clojure.java.jdbc/prepare-statement conn "CREATE SCHEMA foo")))
IllegalArgumentException db-spec conn59: url=jdbc:h2:mem:test user= is missing a required parameter clojure.java.jdbc/get-connection (jdbc.clj:379)

Activity

Hide
Mikkel Gravgaard added a comment -

I should add that I've observed this behaviour on both Microsoft SQL Server and H2 in-memory.

Show
Mikkel Gravgaard added a comment - I should add that I've observed this behaviour on both Microsoft SQL Server and H2 in-memory.
Hide
Sean Corfield added a comment -

Not a bug. You are misusing get-connection. The query and execute! functions expect a "db-spec" but get-connection returns a raw connection object, not a db-spec.

If you require clojure.java.jdbc.spec and then instrument these calls, you'll see them rejected by Spec as a raw connection object does not conform to a db-spec.

In general, you should never call get-connection directly. You should use with-db-connection or similar.

Show
Sean Corfield added a comment - Not a bug. You are misusing get-connection. The query and execute! functions expect a "db-spec" but get-connection returns a raw connection object, not a db-spec. If you require clojure.java.jdbc.spec and then instrument these calls, you'll see them rejected by Spec as a raw connection object does not conform to a db-spec. In general, you should never call get-connection directly. You should use with-db-connection or similar.
Hide
Sean Corfield added a comment -

Release 0.7.7

Show
Sean Corfield added a comment - Release 0.7.7

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: