From 79cc40185ed9e50a1849648842eed2e9c713251e Mon Sep 17 00:00:00 2001 From: Cam Saul <cammsaul@gmail.com> Date: Mon, 8 Jul 2019 15:21:11 -0700 Subject: [PATCH] Use metabase/connection-pool lib (#10306) --- project.clj | 2 +- src/metabase/db.clj | 5 +- src/metabase/db/connection_pool.clj | 77 --------------------- src/metabase/driver/sql_jdbc/connection.clj | 4 +- 4 files changed, 5 insertions(+), 83 deletions(-) delete mode 100644 src/metabase/db/connection_pool.clj diff --git a/project.clj b/project.clj index b2ffe7e60f2..51fac8c50c9 100644 --- a/project.clj +++ b/project.clj @@ -74,7 +74,6 @@ [com.jcraft/jsch "0.1.55"] ; SSH client for tunnels [com.h2database/h2 "1.4.197"] ; embedded SQL database [com.mattbertolini/liquibase-slf4j "2.0.0"] ; Java Migrations lib logging. We don't actually use this AFAIK (?) - [com.mchange/c3p0 "0.9.5.3"] ; connection pooling library [com.taoensso/nippy "2.14.0"] ; Fast serialization (i.e., GZIP) library for Clojure [commons-codec/commons-codec "1.12"] ; Apache Commons -- useful codec util fns [commons-io/commons-io "2.6"] ; Apache Commons -- useful IO util fns @@ -99,6 +98,7 @@ com.sun.jdmk/jmxtools com.sun.jmx/jmxri]] [medley "1.2.0"] ; lightweight lib of useful functions + [metabase/connection-pool "1.0.2"] ; simple wrapper around C3P0. JDBC connection pools [metabase/mbql "1.2.0"] ; MBQL language schema & util fns [metabase/throttle "1.0.1"] ; Tools for throttling access to API endpoints and other code pathways [javax.xml.bind/jaxb-api "2.4.0-b180830.0359"] ; add the `javax.xml.bind` classes which we're still using but were removed in Java 11 diff --git a/src/metabase/db.clj b/src/metabase/db.clj index 2e938c8545c..24f4a5000b8 100644 --- a/src/metabase/db.clj +++ b/src/metabase/db.clj @@ -9,10 +9,9 @@ [clojure.tools.logging :as log] [metabase [config :as config] - [util :as u]] - [metabase.db [connection-pool :as connection-pool] - [spec :as dbspec]] + [util :as u]] + [metabase.db.spec :as dbspec] [metabase.plugins.classloader :as classloader] [metabase.util [date :as du] diff --git a/src/metabase/db/connection_pool.clj b/src/metabase/db/connection_pool.clj deleted file mode 100644 index 75d55f26f47..00000000000 --- a/src/metabase/db/connection_pool.clj +++ /dev/null @@ -1,77 +0,0 @@ -(ns metabase.db.connection-pool - "Low-level logic for creating connection pools for a JDBC-based database. Used by both application DB and connected - data warehouse DBs. - - The aim here is to completely encapsulate the connection pool library we use -- that way we can swap it out if we - want to at some point without having to touch any other files. (TODO - this is currently true of everything except - for the options, which are c3p0-specific -- consider abstracting those as well?)" - (:require [metabase.util :as u] - [metabase.util.schema :as su] - [schema.core :as s]) - (:import com.mchange.v2.c3p0.DataSources - [java.sql Driver DriverManager] - [java.util Map Properties] - javax.sql.DataSource)) - -;;; ------------------------------------------------ Proxy DataSource ------------------------------------------------ - -(defn- proxy-data-source - "Normal c3p0 DataSource classes do not properly work with our JDBC proxy drivers for whatever reason. Use our own - instead, which works nicely." - (^DataSource [^String jdbc-url, ^Properties properties] - (proxy-data-source (DriverManager/getDriver jdbc-url) jdbc-url properties)) - - (^DataSource [^Driver driver, ^String jdbc-url, ^Properties properties] - (reify DataSource - (getConnection [_] - (.connect driver jdbc-url properties)) - (getConnection [_ username password] - (doseq [[k v] {"user" username, "password" password}] - (if (some? k) - (.setProperty properties k (name v)) - (.remove properties k))) - (.connect driver jdbc-url properties))))) - - -;;; ------------------------------------------- Creating Connection Pools -------------------------------------------- - -(defn- map->properties - "Create a `Properties` object from a JDBC connection spec map. Properties objects are maps of String -> String, so all - keys and values are converted to Strings appropriately." - ^Properties [m] - (u/prog1 (Properties.) - (doseq [[k v] m] - (.setProperty <> (name k) (if (keyword? v) - (name v) - (str v)))))) - -(defn- spec->properties ^Properties [spec] - (map->properties (dissoc spec :classname :subprotocol :subname))) - -(def ^:private JDBCSpec - {:subname su/NonBlankString - :subprotocol su/NonBlankString - s/Any s/Any}) - -(s/defn ^:private unpooled-data-source :- DataSource - [{:keys [subname subprotocol], :as spec} :- JDBCSpec] - (proxy-data-source (format "jdbc:%s:%s" subprotocol subname) (spec->properties spec))) - -(defn- pooled-data-source ^DataSource - ([spec] - (DataSources/pooledDataSource (unpooled-data-source spec))) - - ([spec, ^Map pool-properties] - (DataSources/pooledDataSource (unpooled-data-source spec), pool-properties))) - -(def ^{:arglists '([spec] [spec pool-properties-map])} connection-pool-spec - "Create a new connection pool for a JDBC `spec` and return a spec for it. Optionally pass a map of connection pool - properties -- see https://www.mchange.com/projects/c3p0/#configuration_properties for a description of valid options - and their default values." - (comp (fn [x] {:datasource x}) pooled-data-source)) - - -(defn destroy-connection-pool! - "Immediately release all resources held by a connection pool." - [^DataSource pooled-data-source] - (DataSources/destroy pooled-data-source)) diff --git a/src/metabase/driver/sql_jdbc/connection.clj b/src/metabase/driver/sql_jdbc/connection.clj index 5326b7eedab..3dd9ecaab05 100644 --- a/src/metabase/driver/sql_jdbc/connection.clj +++ b/src/metabase/driver/sql_jdbc/connection.clj @@ -4,9 +4,9 @@ (:require [clojure.java.jdbc :as jdbc] [clojure.tools.logging :as log] [metabase + [connection-pool :as connection-pool] [driver :as driver] [util :as u]] - [metabase.db.connection-pool :as connection-pool] [metabase.models.database :refer [Database]] [metabase.util [i18n :refer [tru]] @@ -52,7 +52,7 @@ (defn- destroy-pool! [database-id pool-spec] (log/debug (u/format-color 'red (tru "Closing old connection pool for database {0} ..." database-id))) - (connection-pool/destroy-connection-pool! (:datasource pool-spec)) + (connection-pool/destroy-connection-pool! pool-spec) (when-let [ssh-tunnel (:ssh-tunnel pool-spec)] (.disconnect ^com.jcraft.jsch.Session ssh-tunnel))) -- GitLab