我正在学习使用c3p0进行连接池的教程。https://github.com/clojure/java.jdbc/blob/master/doc/clojure/java/jdbc/ConnectionPooling.md
然后,我尝试使用以下连接运行查询:
(let [db (myapp.db/connection)]
(jdbc/with-connection db)
(jdbc/with-query-results rs ["select * from foo"]
(doseq [row rs]
(println row)))))但是得到这个异常
Exception in thread "main" java.lang.IllegalArgumentException: db-spec {:connection nil, :level 0, :legacy true} is missing a required parameter
at clojure.java.jdbc$get_connection.invoke(jdbc.clj:221)
at clojure.java.jdbc$with_query_results_STAR_.invoke(jdbc.clj:980)
at myapp.db_test$eval604.invoke(myapp_test.clj:12)
at clojure.lang.Compiler.eval(Compiler.java:6619)根据本教程,这是我的myapp.db
(def specification {
:classname "com.mysql.jdbc.Driver"
:subprotocol "mysql"
:subname "//localhost:3306/test"
:user "root"
})
(defn pooled-data-source [specification]
(let [datasource (ComboPooledDataSource.)]
(.setDriverClass datasource (:classname specification))
(.setJdbcUrl datasource (str "jdbc:" (:subprotocol specification) ":" (:subname specification)))
(.setUser datasource (:user specification))
(.setPassword datasource (:password specification))
(.setMaxIdleTimeExcessConnections datasource (* 30 60))
(.setMaxIdleTime datasource (* 3 60 60))
{:datasource datasource}))
(def connection-pool
(delay
(pooled-data-source specification)))
(defn connection [] @connection-pool)提前感谢!
发布于 2013-06-07 00:04:47
jdbc/with-connection在规范之后将您希望在该连接中运行的命令作为参数。您没有在上下文中使用- connection create运行任何命令,而是在其中没有绑定db连接的情况下运行它之外的所有内容。
试试这个版本:
(let [db (myapp.db/connection)]
(jdbc/with-connection db
(jdbc/with-query-results rs ["select * from foo"]
(doseq [row rs]
(println row))))))https://stackoverflow.com/questions/16949840
复制相似问题