Hi,
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.
Sincerely
Meikel
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
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