The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Don't use app DB pool to probe for initial connectivity (#81271)
Rodrigo López Dato7d46650cadba
diff --git a/src/metabase/app_db/setup.clj b/src/metabase/app_db/setup.cljindex 9879703b37c..f517d80cc8a 100644--- a/src/metabase/app_db/setup.clj+++ b/src/metabase/app_db/setup.clj@@ -29,6 +29,7 @@[toucan2.jdbc.options :as t2.jdbc.options][toucan2.pipeline :as t2.pipeline])(:import+ (com.mchange.v2.c3p0 PoolBackedDataSource WrapperConnectionPoolDataSource)(liquibase.exception LockException)))(set! *warn-on-reflection* true)@@ -119,6 +120,20 @@[result] (vals first-row)](= result 1)))+(defn- unpooled-data-source+ "The plain [[javax.sql.DataSource]] a c3p0 pool was built from, or `data-source` unchanged if it isn't a c3p0 pool.++ Connecting through the pool hides why a connection could not be established: c3p0 acquires on background threads, so+ the caller only ever learns that its checkout timed out, while the driver's actual complaint (e.g. Postgres `3D000`,+ \"database ... does not exist\") is logged and dropped. Going direct keeps that exception on the stack, which matters+ for the one-shot connectivity check at startup -- see [[verify-db-connection]]."+ ^javax.sql.DataSource [^javax.sql.DataSource data-source]+ (or (when (instance? PoolBackedDataSource data-source)+ (let [pooled (.getConnectionPoolDataSource ^PoolBackedDataSource data-source)]+ (when (instance? WrapperConnectionPoolDataSource pooled)+ (.getNestedDataSource ^WrapperConnectionPoolDataSource pooled))))+ data-source))+(defn- load-supported-db-versions[](if-let [resource (io/resource "metabase/app_db/supported-db-versions.edn")]@@ -163,7 +178,10 @@data-source :- (ms/InstanceOfClass javax.sql.DataSource)](log/info (u/format-color 'cyan "Verifying %s Database Connection ..." (name db-type)))(let [error-msg (trs "Unable to connect to Metabase {0} DB." (name db-type))]- (try (assert (can-connect-to-data-source? data-source) error-msg)+ ;; deliberately probing the unpooled data source: a failure here is nearly always a misconfiguration, and we want+ ;; the driver's own exception as the cause rather than c3p0's "checkout has timed out". See+ ;; [[unpooled-data-source]].+ (try (assert (can-connect-to-data-source? (unpooled-data-source data-source)) error-msg)(catch Throwable e(throw (ex-info error-msg {} e)))))(with-open [conn (.getConnection ^javax.sql.DataSource data-source)]diff --git a/src/metabase/search/util.clj b/src/metabase/search/util.cljindex dabe0025228..03f32578ef6 100644--- a/src/metabase/search/util.clj+++ b/src/metabase/search/util.clj@@ -52,40 +52,46 @@(def ^:private available-tsv-languages"Mapping of our available locals to the names of the postgres tsvector languages.- Queries the pg_ts_config table to find out which languages are actually available."- (if (= :postgres (mdb/db-type))- (let [default-mapping {:ar :arabic- :ar_SA :arabic- :ca :catalan- :da :danish- :en :english- :fi :finnish- :fr :french- :de :german- :hu :hungarian- :id :indonesian- :it :italian- :nb :norwegian- :pt_BR :portuguese- :ru :russian- :sr :serbian- :es :spanish- :sv :swedish- :tr :turkish}- available-languages (->> (t2/query {:select [:cfgname]- :from [:pg_ts_config]})- (map :cfgname)- (map keyword)- set)]- (into {} (filter (comp available-languages val) default-mapping)))- {}))+ Queries the pg_ts_config table to find out which languages are actually available.++ Computed on first use rather than at namespace-load time: this namespace is required during the boot require chain,+ before [[metabase.app-db.setup/verify-db-connection]] has run, so querying eagerly turns any app DB connection+ problem into an `ExceptionInInitializerError` that buries the real cause."+ (mdb/memoize-for-application-db+ (fn []+ (if (= :postgres (mdb/db-type))+ (let [default-mapping {:ar :arabic+ :ar_SA :arabic+ :ca :catalan+ :da :danish+ :en :english+ :fi :finnish+ :fr :french+ :de :german+ :hu :hungarian+ :id :indonesian+ :it :italian+ :nb :norwegian+ :pt_BR :portuguese+ :ru :russian+ :sr :serbian+ :es :spanish+ :sv :swedish+ :tr :turkish}+ available-languages (->> (t2/query {:select [:cfgname]+ :from [:pg_ts_config]})+ (map :cfgname)+ (map keyword)+ set)]+ (into {} (filter (comp available-languages val) default-mapping)))+ {}))))(defn tsv-language"Get the appropriate text search configuration language for Postgres tsvector operations."[](if-let [custom-language (search.settings/search-language)]custom-language- (if-let [lang ((keyword (i18n/site-locale-string)) available-tsv-languages)]+ (if-let [lang ((keyword (i18n/site-locale-string)) (available-tsv-languages))](name lang)"simple")))diff --git a/test/metabase/app_db/setup_test.clj b/test/metabase/app_db/setup_test.cljindex 18351c80ba0..badd77ec74e 100644--- a/test/metabase/app_db/setup_test.clj+++ b/test/metabase/app_db/setup_test.clj@@ -3,6 +3,7 @@[clojure.java.jdbc :as jdbc][clojure.test :refer :all][metabase.app-db.connection :as mdb.connection]+ [metabase.app-db.connection-pool-setup :as mdb.connection-pool-setup][metabase.app-db.data-source :as mdb.data-source][metabase.app-db.liquibase :as liquibase][metabase.app-db.setup :as mdb.setup]@@ -27,6 +28,19 @@(#'mdb.setup/verify-db-connection :h2 (mdb.data-source/raw-connection-string->DataSource(format "jdbc:h2:mem:%s" (mt/random-name)))))))+(deftest unpooled-data-source-test+ (let [unpooled (mdb.data-source/raw-connection-string->DataSource+ (format "jdbc:h2:mem:%s" (mt/random-name)))]+ (testing "a c3p0 pool is unwrapped back to the data source it was built from"+ (let [pooled (mdb.connection-pool-setup/connection-pool-data-source :h2 unpooled)]+ (is (not (identical? unpooled pooled))+ "sanity check: the pool really is a different object")+ (is (identical? unpooled (#'mdb.setup/unpooled-data-source pooled))+ (str "verify-db-connection must probe the unpooled data source, otherwise c3p0 swallows the driver's "+ "exception and reports a checkout timeout instead"))))+ (testing "a data source that isn't pooled is returned as-is"+ (is (identical? unpooled (#'mdb.setup/unpooled-data-source unpooled))))))+(deftest supported-app-db-version?-test(testing "Should be able to check if an app DB is a supported version"(letfn [(test-supported-versions [db expected-min-version]diff --git a/test/metabase/search/util_test.clj b/test/metabase/search/util_test.cljindex 705f6c440c3..781b1ef7b9d 100644--- a/test/metabase/search/util_test.clj+++ b/test/metabase/search/util_test.clj@@ -71,7 +71,7 @@(deftest available-tsv-languages-test(when (= :postgres (mdb/db-type))- (let [available @#'search.util/available-tsv-languages]+ (let [available (#'search.util/available-tsv-languages)](is (= :english (:en available)))(is (= :german (:de available)))(is (nil? (:ko available))))))