Search code examples
clojurejdbi

No matching method found with JDBI and Clojure


I have a simple DAO ns:

(ns   alavita.dao
  (:require
    [clojure.tools.logging    :as     log     ]
    [clojure.java.io          :as     io      ]
  )
  (:import
    [org.jdbi.v3.core Jdbi Handle]
  ))

(defn create
  (^Jdbi [^String url]
    (Jdbi/create url))
  (^Jdbi [^String url ^String username ^String password]
    (Jdbi/create url username password)))

(defn open
  ^Handle [^Jdbi jdbi]
  (.open jdbi))

When trying to use the lib:

alavita.core=> (def c (dao/create "jdbc:sqlite:/tmp/data.db"))
#'alavita.core/c
alavita.core=> (def h (dao/open c))
#'alavita.core/h
alavita.core=> (.execute h "show tables")

IllegalArgumentException No matching method found: execute for class org.jdbi.v3.core.Handle  clojure.lang.Reflector.invokeMatchingMethod (Reflector.java:53)

This is kind of weird because h definitely has .execute:

alavita.core=> (cli/all-methods h)
(.attach .begin .close .commit .createBatch .createCall .createQuery .createScript .createUpdate .execute .getConfig .getConnection .getExtensionMethod .getStatementBuilder .getTransactionIsolationLevel .inTransaction .isClosed .isInTransaction .isReadOnly .lambda$attach$3 .lambda$new$0 .lambda$useTransaction$1 .lambda$useTransaction$2 .prepareBatch .release .rollback .rollbackToSavepoint .savepoint .select .setConfig .setConfigThreadLocal .setExtensionMethod .setExtensionMethodThreadLocal .setReadOnly .setStatementBuilder .setTransactionIsolation .useTransaction)

Not sure where it goes sideways.

Types for open and create:

alavita.core=> (type (dao/create "jdbc:sqlite:/tmp/data.db"))
org.jdbi.v3.core.Jdbi
alavita.core=> (type (dao/open c))
org.jdbi.v3.core.Handle

Adding reflection:

alavita.core=> (set! *warn-on-reflection* true)
true
alavita.core=> (.execute h "show tables")
Reflection warning, /private/var/folders/nr/g50ld9t91c555dzv91n43bg40000gn/T/form-init767780595230125901.clj:1:1 - call to method execute can't be resolved (target class is unknown).

IllegalArgumentException No matching method found: execute for class org.jdbi.v3.core.Handle  clojure.lang.Reflector.invokeMatchingMethod (Reflector.java:53)

Solution

  • As mentioned in this port, this is an issue with how Java handles var-args. You need to wrap var-args in an array first instead of relying on the var-arg behavior.

    I recommend writing a Clojure function to handle this:

    (defn execute [^Handle h, ^String sql, & args]
      (.execute h sql (into-array Object args)))
    

    And using that instead.