<commit_before>(ns cmr.oracle.connection
 "Contains a record definition that implements the ConcpetStore and Lifecycle protocols
  backed by an Oracle database."
  (:require [cmr.common.lifecycle :as lifecycle]
            [cmr.common.log :refer (debug info warn error)]
            [clojure.java.jdbc :as j])
  (:import com.mchange.v2.c3p0.ComboPooledDataSource))


(defn db-spec
  [db-host db-port db-sid user password]
  {:classname "oracle.jdbc.driver.OracleDriver"
   :subprotocol "oracle"
   :subname (format "thin:@%s:%s:%s" db-host db-port db-sid)
   :user user
   :password password})


(defn test-db-connection!
  "Tests the database connection. Throws an exception if unable to execute some sql."
  [oracle-store]
  (when-not (= [{:a 1M}]
               (j/query oracle-store "select 1 a from dual"))
    (throw (Exception. "Could not select data from database."))))


(defrecord OracleStore [db]

  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  lifecycle/Lifecycle

  (start [this system]
         (test-db-connection! this)
         this)

  (stop [this system]
        this))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defn pool
  [spec]
  (let [cpds (doto (ComboPooledDataSource.)
               (.setDriverClass (:classname spec))
               (.setJdbcUrl (str "jdbc:" (:subprotocol spec) ":" (:subname spec)))
               (.setUser (:user spec))
               (.setPassword (:password spec))
               ;; expire excess connections after 30 minutes of inactivity:
               (.setMaxIdleTimeExcessConnections (* 30 60))
               ;; expire connections after 3 hours of inactivity:
               (.setMaxIdleTime (* 3 60 60)))]
    {:datasource cpds}))

(defn create-db
  "Creates and returns the database connection pool."
  [db-spec]
  (map->OracleStore (pool db-spec)))
<commit_msg>Changed the connection so that it only creates the pool when started.<commit_after>(ns cmr.oracle.connection
 "Contains a record definition that implements the ConcpetStore and Lifecycle protocols
  backed by an Oracle database."
  (:require [cmr.common.lifecycle :as lifecycle]
            [cmr.common.log :refer (debug info warn error)]
            [clojure.java.jdbc :as j])
  (:import com.mchange.v2.c3p0.ComboPooledDataSource))


(defn db-spec
  [db-host db-port db-sid user password]
  {:classname "oracle.jdbc.driver.OracleDriver"
   :subprotocol "oracle"
   :subname (format "thin:@%s:%s:%s" db-host db-port db-sid)
   :user user
   :password password})


(defn test-db-connection!
  "Tests the database connection. Throws an exception if unable to execute some sql."
  [oracle-store]
  (when-not (= [{:a 1M}]
               (j/query oracle-store "select 1 a from dual"))
    (throw (Exception. "Could not select data from database."))))

(defn pool
  [spec]
  (doto (ComboPooledDataSource.)
    (.setDriverClass (:classname spec))
    (.setJdbcUrl (str "jdbc:" (:subprotocol spec) ":" (:subname spec)))
    (.setUser (:user spec))
    (.setPassword (:password spec))
    ;; expire excess connections after 30 minutes of inactivity:
    (.setMaxIdleTimeExcessConnections (* 30 60))
    ;; expire connections after 3 hours of inactivity:
    (.setMaxIdleTime (* 3 60 60))))

(defrecord OracleStore
  [
   ;; The database spec.
   spec

   ;; The database pool of connections
   datasource
   ]

  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  lifecycle/Lifecycle

  (start [this system]
         (let [this (assoc this :datasource (pool spec))]
           (test-db-connection! this)
           this))

  (stop [this system]
        this))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defn create-db
  "Creates and returns the database connection pool."
  [spec]
  (->OracleStore spec nil))

{'fraction_non_alphanumeric': 0.1739360296061327, 'fraction_numerical': 0.008987575997885276, 'mean_word_length': 3.1674008810572687, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7032881', 'n_tokens_mistral': 1297, 'n_tokens_neox': 1141, 'n_words': 347}