【发布时间】:2016-09-16 21:29:47
【问题描述】:
我知道组件和系统(虽然没有使用它们),但我想知道当 init 方法可以从任何线程运行时如何初始化资源。假设我们有 10 个线程,它们都使用 db,并且线程可以按任意顺序启动。这种情况下如何初始化db连接池?
我目前使用比较和设置来执行此操作,但不知何故感觉不对。 我就是这样做的。
(let [datasource (atom nil)]
(defn pooled-conn
"Get a Hikari pooled connection to the database. There will only be one
connection pool for the vm. Additional calls to this function will return
the same connection pool. The connection pool will be created by the first
call to this function"
[datasource-options]
(when (nil? @datasource)
(let [ds (make-datasource datasource-options)]
(when-not (compare-and-set! datasource nil {:datasource ds})
(close-datasource ds))))
@datasource))
我不知道 vm 何时启动,就在我的线程启动时(我正在使用storm,db pool 在某些螺栓中初始化)。有没有更好的方法来做到这一点?
【问题讨论】:
-
您提到了组件和系统,您也可以查看mount 以获得更自然的clojure 感觉。
-
看起来很酷@Shlomi,会去看看
标签: multithreading clojure apache-storm