clojure.java.jdbc, idiomatic way to use a connection

320 views
Skip to first unread message

Kyle Cordes

unread,
Aug 14, 2013, 11:02:52 PM8/14/13
to clo...@googlegroups.com
Hello. I've coded quite a lot of JDBC usage in Java, and enough Clojure to know my way around pretty well; yet I've been unable to figure out the following by reading the source and docs for clojure.java.jdbc. I've read http://clojure.github.io/java.jdbc/ and many pages linked from there.

The question is:

How do I get a connection, then run a series of operations on that same connection? All the API I can find (except for the deprecated, pre-0.3 API) seems to work on a model of: give it a DB connection spec, it connects, runs, and disconnects. Great for playing with a command at a time, less so for doing a series of things that need to happen on the same connection.

--
Kyle Cordes
http://kylecordes.com

Keith Irwin

unread,
Aug 14, 2013, 11:26:31 PM8/14/13
to clo...@googlegroups.com
I think you can use db-connection, something like:

  (let [conn (db-connection spec)
         meta (.getMetaData conn)]
    (doall (.getTables meta nil "schema" "%s" nil))
    (.close conn))

Or what have you. I wrote a little macro `with-meta-data` that was something like that, with added try/catch/finally to make sure the conn got closed. 

Is `with-open` a possibility?

  (with-open [conn (db-connection spec)]
     …)

Would that work?
    
--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Benny Tsai

unread,
Aug 15, 2013, 1:04:25 AM8/15/13
to clo...@googlegroups.com
Does db-transaction work in your case?

"Evaluates body in the context of a transaction on the specified database connection.
The binding provides the database connection for the transaction and the name to which
that is bound for evaluation of the body.
See db-transaction* for more details."

Given a spec, you can use db-transaction like this:

(db-transaction [t-db spec]
  (insert! t-db ...)
  (update! t-db ...)
  ...)

Sean Corfield

unread,
Aug 15, 2013, 1:34:09 AM8/15/13
to clo...@googlegroups.com
The `db-spec` can have a `:connection` member and all operations will
use that. You are responsible for closing it when you're done.
Something like (untested, off the top of my head):

(with-open [conn (get-connection db-spec)]
(let [db (assoc db-spec :connection conn)]
...
(query db ...)
...
(insert! db ...)
...))

This sort of thing needs to be added to the (community-editable)
documentation since it's one of the most frequently asked questions:
http://clojure-doc.org/articles/ecosystem/java_jdbc/home.html

For the most part, the way we use it at World Singles is to define a
pool datasource connection and use that as the db-spec (which _is_
documented at that URL for both c3p0 and BoneCP).

Sean
> --
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@googlegroups.com
> Note that posts from new members are moderated - please be patient with your
> first post.
> To unsubscribe from this group, send email to
> clojure+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to clojure+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.



--
Sean A Corfield -- (904) 302-SEAN
An Architect's View -- http://corfield.org/
World Singles, LLC. -- http://worldsingles.com/

"Perfection is the enemy of the good."
-- Gustave Flaubert, French realist novelist (1821-1880)
Reply all
Reply to author
Forward
0 new messages