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

Merge pull request #568 from metabase/fix_worst_bug_ever

Need to keep a connection open in order to unCLOB-er CLOBs
parents 9c4333c7 ab0a78af
No related branches found
No related tags found
No related merge requests found
......@@ -10,19 +10,23 @@
[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 [database]
(defn- db->connection-spec [{{:keys [short-lived?]} :details, :as database}]
(let [driver (driver/engine->driver (:engine database))
database->connection-details (:database->connection-details driver)
connection-details->connection-spec (:connection-details->connection-spec driver)]
(-> database database->connection-details connection-details->connection-spec)))
(merge (-> database database->connection-details connection-details->connection-spec)
;; unless this is a temp DB, we need to make a pool or the connection will be closed before we get a chance to unCLOB-er the results during JSON serialization
;; TODO - what will we do once we have CLOBS in temp DBs?
(when-not short-lived?
{:make-pool? true}))))
(def ^{:arglists '([database])} db->korma-db
(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..."))
(assoc (kdb/create-db (db->connection-spec database))
:make-pool? true))
(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]
......@@ -67,6 +71,7 @@
(-> (sel :one Table :id 100)
korma-entity
(select (aggregate (count :*) :count)))"
{:arglists '([table] [db table])}
([{db-delay :db, :as table}]
{:pre [(delay? db-delay)]}
(korma-entity @db-delay table))
......
......@@ -33,10 +33,11 @@
(defn- connection-details
"Return a Metabase `Database.details` for H2 database defined by DATABASE-DEFINITION."
[^DatabaseDefinition database-definition]
{:db (format (if (:short-lived? database-definition) "file:%s" ; for short-lived connections don't create a server thread and don't use a keep-alive connection
[^DatabaseDefinition {:keys [short-lived?], :as database-definition}]
{:db (format (if short-lived? "file:%s" ; for short-lived connections don't create a server thread and don't use a keep-alive connection
"file:%s;AUTO_SERVER=TRUE;DB_CLOSE_DELAY=-1")
(filename database-definition))})
(filename database-definition))
:short-lived? short-lived?})
(defn- korma-connection-pool
"Return an H2 korma connection pool to H2 database defined by DATABASE-DEFINITION."
......
......@@ -23,9 +23,10 @@
:TextField "TEXT"
:TimeField "TIME"})
(defn- pg-connection-details [^DatabaseDefinition database-definition]
(merge {:host "localhost"
:port 5432}
(defn- pg-connection-details [^DatabaseDefinition {:keys [short-lived?]}]
(merge {:host "localhost"
:port 5432
:short-lived? short-lived?}
;; HACK
(when (env :circleci)
{:user "ubuntu"})))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment