Hi guys, I was wondering how do you guys usually keep your database connection in Clojure? During my search, I stumbled upon this great question: https://stackoverflow.com/questions/39579023/clojure-best-approach-for-singleton-like-data-connection. But it doesn’t address my concern about the flexibility of the connection created. I wanted the connection to be global in order to be accessed from anywhere so I was thinking def
or defonce
should do it. But I also want to be able to pass values for creating the connection, I don’t want to rely heavily on configuration reading tools like environ etc to determine my connection configuration.
This way I heavily depend on env, which is provided by environ
.
(def conn-options {:jdbc-url (env :database-url)})
(defonce conn (delay (make-datasource conn-options)))
Ideally, I want something like
(defn create-connection [& params] (make-datasource params))
But by using it will result in the connection being created every time the function is invoked. That’s why I was looking for something like a Singleton. I realize maybe a better approach is possible in Clojure, but being a beginner, this is the only way I can think about.