with-connection

68 views
Skip to first unread message

Stuart Campbell

unread,
Feb 6, 2012, 7:32:41 AM2/6/12
to lobos-...@googlegroups.com
Hello,

The README at states:

"You can omit the connection altogether. In that case, actions will use the connection bound by with-connection or the default one."

However, if I do this:

(ns foo
(:use [clojure.java.jdbc :only [with-connection]]
[lobos.core :only [create]]
[lobos.schema :only [table integer]]
[foo.config :only [db]]))

(with-connection db
(create (table :foo (integer :bar)))

I get this exception:

net.sf.log4jdbc.ConnectionSpy cannot be cast to clojure.lang.IPersistentCollection
[Thrown class java.lang.ClassCastException]

clojure.core$conj.invoke(core.clj:83)
clojure.core$merge$fn__3879.invoke(core.clj:2629)
clojure.core$reduce1.invoke(core.clj:879)
clojure.core$reduce1.invoke(core.clj:870)
clojure.core$merge.doInvoke(core.clj:2629)
clojure.lang.RestFn.invoke(RestFn.java:421)
lobos.internal$optional_cnx_or_schema.invoke(internal.clj:82)
lobos.core$create_STAR_.doInvoke(core.clj:119)
clojure.lang.RestFn.invoke(RestFn.java:423)
...

Am I misunderstanding something?

Thanks,
Stuart

Stuart Campbell

unread,
Feb 7, 2012, 3:29:58 AM2/7/12
to lobos-...@googlegroups.com
I should mention that the equivalent clojure.java.jdbc call works fine, i.e.:

  (use '[clojure.java.jdbc :only [with-connection create-table]])

  (with-connection db
    (create-table :foo [:bar "integer"]))

and that I'm using 1.0.0-SNAPSHOT with Clojure 1.3.

Nicolas Buduroi

unread,
Feb 7, 2012, 11:30:28 AM2/7/12
to Lobos Library
Yes, this is definitely a bug, I'll have a look at it later today.

On Feb 7, 3:29 am, Stuart Campbell <stuart.william.campb...@gmail.com>
wrote:
> I should mention that the equivalent clojure.java.jdbc call works fine,
> i.e.:
>
>   (use '[clojure.java.jdbc :only [with-connection create-table]])
>
>   (with-connection db
>     (create-table :foo [:bar "integer"]))
>
> and that I'm using 1.0.0-SNAPSHOT with Clojure 1.3.
>
> On 6 February 2012 23:32, Stuart Campbell <stuart.william.campb...@gmail.com

Nicolas Buduroi

unread,
Feb 7, 2012, 8:21:08 PM2/7/12
to Lobos Library
This will take more time to fix than I thought. The java.jdbc connectivity API has changed quite a bit and accept more types of connections. I've made a rather poor design decision by assuming the db-spec would always be a map, so lobos connectivity will need some major changes. I'll see what I can do this week-end.

In the meantime could you provide the way you create the db-spec you're using. I see from the error message that it is a wrapper around a connection and I still don't get how java.jdbc handle that case, I'll need to do more research.

Stuart Campbell

unread,
Feb 7, 2012, 9:50:31 PM2/7/12
to lobos-...@googlegroups.com
Hi Nicolas,

java.jdbc doesn't do anything special with the wrapper. I'm using log4jdbc, which handles the proxying automatically.

I get the same result when using the underlying Derby driver, which would be defined like so:

(def db {:classname "org.apache.derby.jdbc.EmbeddedDriver"
         :subprotocol "derby"
         :subname "c:/temp/derby"
         :create true})

Thanks,
Stuart

Nicolas Buduroi

unread,
Feb 8, 2012, 8:10:48 PM2/8/12
to lobos-...@googlegroups.com
Well, that is strange. I wasn't able to fully test Lobos with Derby because it isn't supported yet (I'll probably do that this weekend) but I was able to get to the compiler and the derby files were created. Can try out with H2 to see if you get the same problem?

Stuart Campbell

unread,
Feb 8, 2012, 8:37:29 PM2/8/12
to lobos-...@googlegroups.com
I just tried it, and I do get the same problem. I've traced the problem through the source below, just to make sure we're looking at the same version of the code.

In lobos.internal:

(defn optional-cnx-or-schema [args]
  (let [[cnx-or-schema args]
        (optional #(or (schema/schema? %)
                       (and (-> % schema/definition? not)
                            (conn/connection? %))) args)
        args* args
        schema (when (schema/schema? cnx-or-schema) cnx-or-schema)
        cnx (or (conn/find-connection)
                (-> schema :options :db-spec)
                (when-not schema cnx-or-schema)
                :default-connection)
        db-spec (merge (conn/get-db-spec cnx)
                       (when schema
                         {:schema (-> schema :sname name)}))]
    [db-spec schema args]))

conn/find-connection returns something like #<JdbcConnection conn9: url=jdbc:h2:tmp/h2 user=>. This goes into conn/get-db-spec and comes out unchanged:

(defn get-db-spec
  "Returns the associated db-spec or itself. *For internal use*."
  [& [connection-info]]
  (let [connection-info (or connection-info :default-connection)]
    (or (:db-spec sqlint/*db*)
        (if (keyword? connection-info)
          (-> @global-connections connection-info :db-spec)
          connection-info))))

 which causes the exception in the call to (merge).

Thanks for looking into this. I didn't realise Derby wasn't supported yet - whoops!

Regards,
Stuart

Nicolas Buduroi

unread,
Feb 12, 2012, 4:08:22 PM2/12/12
to lobos-...@googlegroups.com
Ok, I got it! You are using the java.jdbc version of `with-connection`, while I was refering to the `lobos.connectivity/with-connection` macro. I updated the readme to be more precise.

Also I'm not sure I'll get the Derby support working today finally.

Stuart Campbell

unread,
Feb 13, 2012, 4:36:46 AM2/13/12
to lobos-...@googlegroups.com
Ah, that's it! Thanks for your help.
Reply all
Reply to author
Forward
0 new messages