I’m quite new to Stuart Sierra components and how properly work with them. In my project I’ve created database component that looks like this:
(defrecord Database [connection] component/Lifecycle (start [component] (migrate) (let [conn (or connection (jdbc/get-connection (env :database-url)))] (assoc component :connection conn))) (stop [component] (when-let [conn (:connection component)] (.close conn)) (assoc component :connection nil)))
I have two worries (or misunderstandings) about such component.
- If the db connection is created on start and close on stop, does it mean that during life of my web application (it can be quite long) I have always one connection opened? What would happen when two separate threats would use such connection?
- Could you suggest me how could I rewrite this code to add a db connection pooling? For your info, I use Postgresql db.
I appreciate for any help. Thanks in advance.