I was trying to construct a simple example of what I actually have in my apps that use pooling on top of java.jdbc. My actual code *does* work to create a singleton but you're right, I've contracted my code too far in trying to create a simple example of it... I'll have another attempt!
Sean On Aug 22, 3:04 am, "Meikel Brandmeyer (kotarak)" <m...@kotka.de> wrote: > Am Montag, 22. August 2011 11:53:32 UTC+2 schrieb faenvie: > > yesterday i came across the following use of > > the delay-macro: > > > (defn pooled-data-source > > [db-connection-settings] > > ; this Fn creates and returns object of type PooledDataSource > > ) > > > (defn pooled-data-source-as-singleton > > [db-connection-settings] > > (let [datasource (delay (pooled-data-source db-connection- > > settings))] > > @datasource)) > > > i am not sure, what the exact semantic of this is and if > > it's safe in a multithreaded context. > > > What is the difference to using def/defonce ? > > > Someone who can explain ? > > This snippet does not work as the original author intended. You can remove > the whole delay incantations and everything will work as before. That's > because each call of pooled-data-source-as-singleton creates a new delay, > forces it and throws it away immediately. What probably was intended, is the > use of memoize: > > (def ^{:arglists ([db-connection-settings])} pooled-data-source-as-singleton > (memoize pooled-data-source)) > > The intention is to delay the creation of the datapool instance until > runtime and not do it on load time. This is also nice if you have your db > connection configurable in some non-code form. If you have only one db > connection, which is hardwired in some Var at load time, you can go with the > following: > > (let [db-connection-pool (delay (pooled-data-source > db-connection-settings))] > (defn pooled-data-source-as-singleton > [] > @db-connection-pool)) > > This is maybe, what the original author had in mind. > > Hope, I'm not too far off. -- 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 Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en