java.lang.Exception: A global connection by that name already exists (:default-connection) (config.clj:1)

148 views
Skip to first unread message

J. Pablo Fernández

unread,
Aug 18, 2011, 6:01:38 AM8/18/11
to lobos-...@googlegroups.com
Hello,

I have a simple lobos.config that opens the global connection, like this:

(ns lobos.config
  (:require lobos.connectivity))

(def db
  {:classname "org.postgresql.Driver"
   :subprotocol "postgresql"
   :subname "//localhost:5432/mgr"})

(lobos.connectivity/open-global db)

The problem I have is that every time I re-load that file, I get the error "java.lang.Exception: A global connection by that name already exists (:default-connection) (config.clj:1)". How do you avoid this problem?

Nicolas Buduroi

unread,
Aug 18, 2011, 10:57:38 AM8/18/11
to lobos-...@googlegroups.com
You could look if there's a global default connection (or any other) by looking at the global-connections var like this:

    (@global-connections :default-connection)

But in practice the config file shouldn't be reloaded very often. In this case you could always use close-global manually before reloading it.

2011/8/18 J. Pablo Fernández <pup...@pupeno.com>

J. Pablo Fernández

unread,
Aug 18, 2011, 11:28:01 AM8/18/11
to lobos-...@googlegroups.com
I found myself loading lobos.config fairly regularly if only because I'm loading others files that load it. With your reply I ended up constructing an open-global-when-necessary which seems to work:

(ns lobos.config
  (:require lobos.connectivity))

(defn open-global-when-necessary
  "Open a global connection only when necessary, that is, when no previous
  connection exist or when db-spec is different to the current global
  connection"
  [db-spec]
  ;; If the connection credentials has changed, close the connection.
  (when (and (@lobos.connectivity/global-connections :default-connection)
          (not= (:db-spec (@lobos.connectivity/global-connections :default-connection)) db-spec))
    (lobos.connectivity/close-global))
  ;; Open a new connection or return the existing one.
  (if (nil? (@lobos.connectivity/global-connections :default-connection))
    (lobos.connectivity/open-global db-spec)
    (@lobos.connectivity/global-connections :default-connection)))

(def db
  {:classname "org.postgresql.Driver"
   :subprotocol "postgresql"
   :subname "//localhost:5432/mgr"})

(open-global-when-necessary db)


J. Pablo Fernández

unread,
Aug 18, 2011, 11:53:58 AM8/18/11
to lobos-...@googlegroups.com
Something else is going on. I load and re-load this improved version of the file and it works as expected:

user=> (load-file "/Users/pupeno/Projects/mgr/src/lobos/config.clj")
{:connection #<Jdbc4Connection org.postgresql.jdbc4.Jdbc4Connection@94f1726>, :db-spec {:classname "org.postgresql.Driver", :subprotocol "postgresql", :subname "//localhost:5432/mgr"}}
user=> (load-file "/Users/pupeno/Projects/mgr/src/lobos/config.clj")
{:connection #<Jdbc4Connection org.postgresql.jdbc4.Jdbc4Connection@94f1726>, :db-spec {:classname "org.postgresql.Driver", :subprotocol "postgresql", :subname "//localhost:5432/mgr"}}
user=> (load-file "/Users/pupeno/Projects/mgr/src/lobos/config.clj")
{:connection #<Jdbc4Connection org.postgresql.jdbc4.Jdbc4Connection@94f1726>, :db-spec {:classname "org.postgresql.Driver", :subprotocol "postgresql", :subname "//localhost:5432/mgr"}}

but when I load migrations.clj, I get the error:

user=> (load-file "/Users/pupeno/Projects/mgr/src/lobos/migrations.clj")
java.lang.Exception: A global connection by that name already exists (:default-connection) (config.clj:1)

The contents of migrations.clj is this:

(ns lobos.migrations
  (:refer-clojure :exclude [alter defonce drop bigint boolean char double float time])
  (:use (lobos [migration :only [defmigration]] core schema)
        lobos.config)
  (:require lobos.connectivity
            lobos.core
            [clojureql.core :as clq]))

;(println @(clq/table db :foo))

(defmigration create-admins
  (up [] (println "up"))
  (down [] (println "down")))

(lobos.core/run)

Any ideas what might be going on here? Something else creating the default connection?

PS: why don't I get stack traces? this is driving me crazy

Nicolas Buduroi

unread,
Aug 19, 2011, 12:39:14 AM8/19/11
to lobos-...@googlegroups.com
I'm not able to reproduce this issue, but you don't actually need to load the migration file, the run command can do that for you.

Also you shouldn't include the run command into the migrations file, it doesn't throw any error in my test, but the migrations aren't loaded for some reason that escape me.

For example, using your migrations file without the call to run:

user=> (use '(lobos core config))
WARNING: ...
nil
user=> (run)
create-admins
up
nil
user=> (rollback)
create-admins
down
nil

For now you'll have to require the config file (or set a connection in another way) before running the migration commands, but I'll probably automate this process once I'll decide if the config file is a good idea or not.

P.S.: I'll try to improve the documentation this week-end, but there's still some decisions to be made about the migration API. I might go with what was done as it is as there were no complaints. I'm still open to suggestions as I probably won't release 0.8 until the end of summer.

2011/8/18 J. Pablo Fernández <pup...@pupeno.com>
Something else is going on. I load and re-load this improved version of the file and it works as expected:

J. Pablo Fernández

unread,
Aug 19, 2011, 1:57:57 AM8/19/11
to lobos-...@googlegroups.com
The reason why I put the run command temporary in that files is because errors generated from a file tend to be a little bit better than from the REPL.

If you are going to automate stuff, I think having a lein plugin to do lein db-migrate, a-la Rails, would be really nice.
Reply all
Reply to author
Forward
0 new messages