我不认为 with-connection 保持开放。Doc 或 source 建议它,在 2.3 中,我能够通过在运行查询后检查db来确认它。源代码如下所示:
(defn with-connection*
"Evaluates func in the context of a new connection to a database then
closes the connection."
[db-spec func]
(with-open [^java.sql.Connection con (get-connection db-spec)]
(binding [*db* (assoc *db* :connection con :level 0 :rollback (atom false))]
(func))))
连接池在懒惰地创建它们时可能很有用,但这并不能使它们保持打开状态。使连接可设置似乎是必要的。然而,最新的 API 强调只是创建连接并将其传递给每个调用。虽然仍然是 ALPHA,但这看起来像是这个库的未来(并且仍然与连接池兼容)。从图书馆的维基:
(require '[clojure.java.jdbc :as j]
'[clojure.java.jdbc.sql :as s])
(def mysql-db {:subprotocol "mysql"
:subname "//127.0.0.1:3306/clojure_test"
:user "clojure_test"
:password "clojure_test"})
(j/insert! mysql-db :fruit
{:name "Apple" :appearance "rosy" :cost 24}
{:name "Orange" :appearance "round" :cost 49})