Skip to content
Snippets Groups Projects
Commit 81c38412 authored by Cam Saül's avatar Cam Saül
Browse files

Merge pull request #724 from metabase/less_dumb_memoization

less-dumb memoization
parents 4a527334 c392dbe5
No related branches found
No related tags found
No related merge requests found
(ns metabase.driver.generic-sql.util
"Shared functions for our generic-sql query processor."
(:require [clojure.core.memoize :as memo]
[clojure.java.jdbc :as jdbc]
(:require [clojure.java.jdbc :as jdbc]
[clojure.tools.logging :as log]
[colorize.core :as color]
[korma.core :as korma]
......@@ -9,7 +8,6 @@
[metabase.driver :as driver]
[metabase.driver.query-processor :as qp]))
;; Cache the Korma DB connections for a given Database for 60 seconds instead of creating new ones every single time
(defn- db->connection-spec [{{:keys [short-lived?]} :details, :as database}]
(let [driver (driver/engine->driver (:engine database))
database->connection-details (:database->connection-details driver)
......@@ -23,14 +21,17 @@
(def ^{:arglists '([database])}
db->korma-db
"Return a Korma database definition for DATABASE.
This does a little bit of smart caching (for 60 seconds) to avoid creating new connections when unneeded."
(let [-db->korma-db (memo/ttl (fn [database]
(log/debug (color/red "Creating a new DB connection..."))
(kdb/create-db (db->connection-spec database)))
:ttl/threshold (* 60 1000))]
;; only :engine and :details are needed for driver/connection so just pass those so memoization works as expected
(fn [database]
(-db->korma-db (select-keys database [:engine :details])))))
Since Korma/C3PO seems to be bad about cleaning up its connection pools, this function is
memoized and will return an existing connection pool on subsequent calls."
(let [db->korma-db (fn [database]
(log/debug (color/red "Creating a new DB connection..."))
(kdb/create-db (db->connection-spec database)))
memoized-db->korma-db (memoize db->korma-db)]
(fn [{{:keys [short-lived?]} :details, :as database}]
;; Use un-memoized version of function for so-called "short-lived" databases (i.e. temporary ones that we won't create a connection pool for)
((if short-lived?
db->korma-db
memoized-db->korma-db) (select-keys database [:engine :details]))))) ; only :engine and :details are needed for driver/connection so just pass those so memoization works as expected
(def ^:dynamic ^java.sql.DatabaseMetaData *jdbc-metadata*
"JDBC metadata object for a database. This is set by `with-jdbc-metadata`."
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment