2013-06-05 70 views
4

我正在研究一個需要與數據庫交談的Web服務,所以我正在模擬我的基本庫以使我可以訪問我的桌面上的postgres。Clojure JDBC:無法找到Postgres驅動程序

Jun 5, 2013 1:27:46 PM com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask run 
WARNING: [email protected]da -- Acquisition  
Attempt Failed!!! Clearing pending acquires. 
While trying to acquire a needed new resource, we failed to succeed more than the maximum number of allowed acquisition attempts (30). 
Last acquisition attempt exception: 
java.sql.SQLException: No suitable driver 
at java.sql.DriverManager.getDriver(DriverManager.java:264) 

在我的數據庫庫,我有以下

(ns myapp.db 
    (:import [com.mchange.v2.c3p0 ComboPooledDataSource])) 

(def specification { 
    :classname "postgresql" 
    :subprotocol "org.postgresql.Driver" 
    :subname "//localhost:5432;database=test" 
}) 

(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) 
在我的單元測試

然後:

(ns myapp.db-test 
    (:use clojure.test) 
    (:require [myapp.db] 
       [clojure.java.jdbc :as jdbc])) 

(let [db (myapp.db/connection)] 
    (jdbc/with-connection db) 
     (jdbc/with-query-results rs ["select * from foo"] 
      (doseq [row rs] 
       (println row))))) 

但是這並在REPL工作,所以至少我知道數據庫並接受連接:

user=> (require '[clojure.java.jdbc :as sql]) 
user=> (sql/with-connection "postgresql://localhost:5432/test" 
      (sql/with-query-results results ["select * from foo"] 
       (doseq [result results] (println result)))) 
{:y 2, :x 1} 
nil 
user=> 

對此的幫助非常感謝!

我project.clj如下

(defproject myapp "0.1.0" 
    :description "myapp" 
    :dependencies [ 
     [org.clojure/clojure "1.5.1"] 
     [org.clojure/java.jdbc "0.3.0-alpha4"] 
     [postgresql "9.1-901.jdbc4"] 
     [c3p0/c3p0 "0.9.1.2"]]) 

回答

2

我建議,以解決您規格

(def specification { 
    :subprotocol "postgresql" 
    :classname "org.postgresql.Driver" 
    :subname "//localhost:5432/test"}) 
2

可能存在其他一些問題(我這人非常Clojure的新手),但你絕對有:類名和:子協議在您的規格反轉。 :classname應該是「org.postgresql.Driver」。 :子協議應該是「postgresql」。