diff --git a/src/metabase/db.clj b/src/metabase/db.clj index fed3f83ece1d93dcac7887483e66cf085e3d5de8..2f7a70b983b36de917e6f559f9ca766a38cdd1a4 100644 --- a/src/metabase/db.clj +++ b/src/metabase/db.clj @@ -135,39 +135,53 @@ ;;; | DB CONNECTION / TRANSACTION STUFF | ;;; +------------------------------------------------------------------------------------------------------------------------+ -(set! *warn-on-reflection* true) - (defn connection-pool "Create a connection pool for the given database spec." - [{:keys [subprotocol subname classname], :as spec}] + [{:keys [subprotocol + subname + classname + excess-timeout + idle-timeout + initial-pool-size + minimum-pool-size + maximum-pool-size + test-connection-query + idle-connection-test-period + test-connection-on-checkin + test-connection-on-checkout] + :or {excess-timeout (* 30 60) + idle-timeout (* 3 60 60) + initial-pool-size 3 + minimum-pool-size 3 + maximum-pool-size 15 + test-connection-query nil + idle-connection-test-period 0 + test-connection-on-checkin false + test-connection-on-checkout false} + :as spec}] {:datasource (doto (ComboPooledDataSource.) - (.setDriverClass classname) - (.setJdbcUrl (str "jdbc:" subprotocol ":" subname)) - (.setMaxIdleTimeExcessConnections (* 30 60)) - (.setMaxIdleTime (* 3 60 60)) - (.setInitialPoolSize 3) - (.setMinPoolSize 3) - (.setMaxPoolSize 15) - (.setIdleConnectionTestPeriod 0) - (.setTestConnectionOnCheckin false) - (.setTestConnectionOnCheckout false) - (.setPreferredTestQuery nil) - (.setProperties (u/prog1 (java.util.Properties.) - (doseq [[k v] (dissoc spec - :make-pool? :classname :subprotocol :subname - :naming :delimiters :alias-delimiter - :excess-timeout :idle-timeout - :initial-pool-size :minimum-pool-size :maximum-pool-size - :test-connection-query - :idle-connection-test-period - :test-connection-on-checkin - :test-connection-on-checkout)] - (.setProperty <> (name k) (str v))))))}) - -(defn delay-pool - "Return a delay for creating a connection pool for the given spec." - [spec] - (delay (connection-pool spec))) + (.setDriverClass classname) + (.setJdbcUrl (str "jdbc:" subprotocol ":" subname)) + (.setMaxIdleTimeExcessConnections excess-timeout) + (.setMaxIdleTime idle-timeout) + (.setInitialPoolSize initial-pool-size) + (.setMinPoolSize minimum-pool-size) + (.setMaxPoolSize maximum-pool-size) + (.setIdleConnectionTestPeriod idle-connection-test-period) + (.setTestConnectionOnCheckin test-connection-on-checkin) + (.setTestConnectionOnCheckout test-connection-on-checkout) + (.setPreferredTestQuery test-connection-query) + (.setProperties (u/prog1 (java.util.Properties.) + (doseq [[k v] (dissoc spec + :make-pool? :classname :subprotocol :subname + :naming :delimiters :alias-delimiter + :excess-timeout :idle-timeout + :initial-pool-size :minimum-pool-size :maximum-pool-size + :test-connection-query + :idle-connection-test-period + :test-connection-on-checkin + :test-connection-on-checkout)] + (.setProperty <> (name k) (str v))))))}) (defn create-db "Create a db connection object manually instead of using defdb. This is often @@ -179,7 +193,7 @@ If the spec includes `:make-pool? true` makes a connection pool from the spec." [spec] {:pool (if (:make-pool? spec) - (delay-pool spec) + (delay (connection-pool spec)) spec)}) (def _connection diff --git a/src/metabase/driver/generic_sql.clj b/src/metabase/driver/generic_sql.clj index 3babf3aa72449ac271cae50f661582cc7d6bef8f..733c30e9f27db9ee8b125dd95cd20803877bbb8c 100644 --- a/src/metabase/driver/generic_sql.clj +++ b/src/metabase/driver/generic_sql.clj @@ -5,13 +5,13 @@ [clojure.tools.logging :as log] (honeysql [core :as hsql] [format :as hformat]) - [korma.db :as kdb] - [metabase.driver :as driver] - [metabase.sync-database.analyze :as analyze] - metabase.query-processor.interface + (metabase [db :as db] + [driver :as driver]) (metabase.models [field :as field] raw-table [table :as table]) + metabase.query-processor.interface + [metabase.sync-database.analyze :as analyze] [metabase.util :as u] [metabase.util.honeysql-extensions :as hx]) (:import java.sql.DatabaseMetaData @@ -117,11 +117,12 @@ [{:keys [id engine details]}] (log/debug (u/format-color 'magenta "Creating new connection pool for database %d ..." id)) (let [spec (connection-details->spec (driver/engine->driver engine) details)] - (kdb/connection-pool (assoc spec :minimum-pool-size 1 - ;; prevent broken connections closed by dbs by testing them every 3 mins - :idle-connection-test-period (* 3 60) - ;; prevent overly large pools by condensing them when connections are idle for 15m+ - :excess-timeout (* 15 60))))) + (db/connection-pool (assoc spec + :minimum-pool-size 1 + ;; prevent broken connections closed by dbs by testing them every 3 mins + :idle-connection-test-period (* 3 60) + ;; prevent overly large pools by condensing them when connections are idle for 15m+ + :excess-timeout (* 15 60))))) (defn- notify-database-updated "We are being informed that a DATABASE has been updated, so lets shut down the connection pool (if it exists) under diff --git a/src/metabase/models/interface.clj b/src/metabase/models/interface.clj index 79d611560cfd8fe0bdf02cf0a0b2b810678e0b6c..6cf5d256f0e3f3e3c12516b4ec6313ea925c2dca 100644 --- a/src/metabase/models/interface.clj +++ b/src/metabase/models/interface.clj @@ -1,7 +1,6 @@ (ns metabase.models.interface (:require [clojure.tools.logging :as log] [cheshire.core :as json] - [korma.core :as k] (metabase [config :as config] [util :as u]) [metabase.models.common :as common])) @@ -296,7 +295,6 @@ :arglists ''([] [id] [& kvs]) :doc (or docstr (format "Entity for '%s' table; instance of %s." (name table-name) instance))) - (-> (k/create-entity ~(name entity)) - (k/table ~table-name) - (assoc ::entity true) - ~map->instance))))) + (~map->instance {:table ~table-name + :name ~(name entity) + ::entity true}))))) diff --git a/test/metabase/test/data/generic_sql.clj b/test/metabase/test/data/generic_sql.clj index 73583235394b5b073c17c602c7254393589b6e34..236bca370c091e4ebfbf55fb9f4e083fccde844a 100644 --- a/test/metabase/test/data/generic_sql.clj +++ b/test/metabase/test/data/generic_sql.clj @@ -6,8 +6,6 @@ (honeysql [core :as hsql] [format :as hformat] [helpers :as h]) - (korma [core :as k] - [db :as kdb]) [medley.core :as m] [metabase.driver :as driver] [metabase.driver.generic-sql :as sql] diff --git a/test/metabase/test/data/h2.clj b/test/metabase/test/data/h2.clj index 8584671e1e6d3c6ffda9eb6ae8eccb73d645828a..a80f29c0eabe49b1ebe209ba2ccc182089ddb9f0 100644 --- a/test/metabase/test/data/h2.clj +++ b/test/metabase/test/data/h2.clj @@ -3,8 +3,7 @@ ;; TODO - rework this namespace to use `u/drop-first-arg` where appropriate (:require [clojure.core.reducers :as r] [clojure.string :as s] - (korma [core :as k] - [db :as kdb]) + [metabase.db.spec :as dbspec] metabase.driver.h2 (metabase.test.data [generic-sql :as generic] [interface :as i]) @@ -66,7 +65,7 @@ (merge mixin {:create-db-sql (constantly create-db-sql) :create-table-sql create-table-sql - :database->spec (comp kdb/h2 i/database->connection-details) ; Don't use the h2 driver implementation, which makes the connection string read-only & if-exists only + :database->spec (comp dbspec/h2 i/database->connection-details) ; Don't use the h2 driver implementation, which makes the connection string read-only & if-exists only :drop-db-if-exists-sql (constantly nil) :execute-sql! (fn [this _ dbdef sql] ;; we always want to use 'server' context when execute-sql! is called