gpt4 book ai didi

sql-server - Clojure/SQLServer : How to Call Stored Procedure with C3P0 Connection Pool

转载 作者:行者123 更新时间:2023-12-03 03:24:18 25 4
gpt4 key购买 nike

按照这个例子

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)

我了解如何使用连接来进行选择和插入语句等,我的问题是如何使用它来调用存储过程并收集输出,这些输出可能是各种形状和大小的记录?

;; ### 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("...")}"))

最佳答案

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

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

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

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

关于sql-server - Clojure/SQLServer : How to Call Stored Procedure with C3P0 Connection Pool,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17437220/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com