Clojure / SQLServer:如何使用C3P0连接池调用存储过程

时间:2013-07-02 23:03:37

标签: sql-server clojure c3p0

遵循此示例

https://github.com/clojure/java.jdbc/blob/master/doc/clojure/java/jdbc/ConnectionPooling.md

的jdbc连接池,我已经在Clojure应用程序中为SQLServer建立了一个连接池,如下所示

;; Database Connection Handling.
(ns myapp.db
    (:import [com.mchange.v2.c3p0 ComboPooledDataSource]))

;; ### specification
;; Defines the database connection parameters.
(def specification {
    :classname "com.microsoft.sqlserver.jdbc.SQLServerDriver"
    :subprotocol "sqlserver"
    :subname "//some;info;here"
})

;; ### pooled-data-source
;; Creates a database connection pool using the
;; <a href="https://github.com/swaldman/c3p0">c3p0</a> JDBC
;; connection pooling library.
(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}))

;; ### connection-pool
;; Creates the connection pool when first called.
(def connection-pool
    (delay
        (pooled-data-source specification)))

;; ### connection
;; Get a connection from the connection pool.
(defn connection [] @connection-pool)

我得到如何使用连接来制作select和insert语句等,我的问题是如何使用它来调用存储过程并收集输出,这可能是各种形状和大小的记录?

;; ### Definitions of queries.
(ns myapp.query
     (:require [myapp.db]))

;; HOW DO I CALL THIS PROC THROUGH A POOLED CONNECTION?
(defn call-the-stored-proc []
    (str "{ call someStoredProcForMyApp("...")}"))

1 个答案:

答案 0 :(得分:2)

对于不需要OUT参数的基本存储过程,您可以使用db-do-prepared

(require '[clojure.java.jdbc :as j])
(j/db-do-prepared (connection) "EXEC YourStoredProc ?" [COLUMN_NAME])

它会调用您的connection函数,该函数与文档说的相同。

我已经开始为JDBC添加完整的可调用语句支持,但我没有时间完成工作。这是Clojue的JIRA中的问题JDBC-48,以及我的进度is in the sprocs branch of my fork