seancorfield / next-jdbc

A modern low-level Clojure wrapper for JDBC-based access to databases.
https://cljdoc.org/d/com.github.seancorfield/next.jdbc/
Eclipse Public License 1.0
755 stars 90 forks source link

execute-batch! cannot use options-wrapped pooled connection #181

Closed seancorfield closed 2 years ago

seancorfield commented 2 years ago

Describe the bug If you use execute-batch! on the result of with-options inside a Connection context, such as with-transaction, you get a protocol implementation error because it tries to call get-datasource on the underlying Connection.

To Reproduce

user=> (require '[next.jdbc :as jdbc])
nil
user=> (require '[next.jdbc.connection :as conn])
nil
user=> (def db-spec {:dbtype "mysql" :dbname "worldsingles" :user "..." :password "..."})
#'user/db-spec
user=> (def ds (conn/->pool com.mchange.v2.c3p0.ComboPooledDataSource db-spec))
#'user/ds
user=> (def dso (jdbc/with-options ds {}))
#'user/dso
user=> (jdbc/with-transaction [tx dso]
  #_=>   (let [ds (jdbc/with-options tx {})]
  #_=>     (jdbc/execute-batch! ds "some SQL" [] {})))
Execution error (IllegalArgumentException) at next.jdbc.protocols/eval7116$fn$G (protocols.clj:14).
No implementation of method: :get-datasource of protocol: #'next.jdbc.protocols/Sourceable found for class: com.mchange.v2.c3p0.impl.NewProxyConnection

Expected behavior It should create a PreparedStatement from the DefaultOptions-wrapped Connection.

Reported ClojureVerse: next.jdbc applying defaults on transaction causes execute-batch! to fail when using c3p0 connection pool