The SQL library in clojure-contrib already does this just for that sole
purpose:

(defmacro with-connection
  "Evaluates body in the context of a new connection to a database then
  closes it. db-spec is a map containing string values for these
required
  keys:
    :classname     the jdbc driver class name
    :subprotocol   the jdbc subprotocol
    :subname       the jdbc subname
  db-spec may contain additional key-value pairs that are passed along
to
  the driver as properties such as :user, :password, etc."
  [db-spec & body]
  `(do
     (clojure.lang.RT/classForName (:classname ~db-spec))
     (with-open con#

...

I defined the database with a definition like this:

(def dbmaster {
  :classname "com.mysql.jdbc.Driver"
  :subprotocol "mysql"
...

but I was always ending up with an exception saying it could not find
the driver.

As far as I can see the class name is right. The call to classForName
did not trigger any exceptions.
The classForName call should trigger execution of the static code in the
MySql driver class.

So for now it works with my workaround (registering the driver directly)
but I'll try to get to the bottom of this eventually.

Thank you all for the suggestions.

Luc


Now I still did not understand why it was not working when I was calling
the library 
On Fri, 2008-10-10 at 02:45 -0700, mritun wrote:

> 
> 
> On Oct 10, 1:46 am, Luc Prefontaine <[EMAIL PROTECTED]>
> wrote:
> > I found a workaround...
> >
> > I was able to register the driver using
> > -Djdbc.drivers=com.mysql.jdbc.Driver on the java command line.
> > Looks like loading the class is not enough, adding the above does work:
> >
> > user=> (enumeration-seq (. java.sql.DriverManager getDrivers))
> > ([EMAIL PROTECTED] [EMAIL PROTECTED])
> >
> > The mysql jdbc driver code says that it registers itself (static block)
> > but something is wrong.
> > Eventually I will find why but now I have other pressing needs to full
> > fill than debugging that code.
> 
> Static blocks don't execute until the class is loaded, so just
> specifying a class in classpath is not enough.
> 
> In Java the idiomatic way to let the driver initialize itself, is to
> add the following line somewhere in your initialization code before
> requesting connection:
> 
> Class.forName("com.mysql.jdbc.Driver");
> 
> This loads the class, and its static block registers the driver. You'd
> have to do an equivalent in clojure too.
> 
> - Akhilesh
> > 
> 

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to