Skip to content

Commit

Permalink
Update part5
Browse files Browse the repository at this point in the history
  • Loading branch information
quan-nh committed Feb 22, 2024
1 parent 810aea4 commit 81eaa0f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 24 deletions.
28 changes: 5 additions & 23 deletions doc/clj-web-from-the-ground-up-5.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
add config

`[aero "1.1.3"]`
`[aero "1.1.6"]`

`resources\config.edn`
```clj
{:db-spec
{:classname "org.h2.Driver"
:subprotocol "h2:mem" ; the prefix `jdbc:` is added automatically
:subname "demo;DB_CLOSE_DELAY=-1" ; `;DB_CLOSE_DELAY=-1` very important!!!
; http://www.h2database.com/html/features.html#in_memory_databases
:user "sa" ; default "system admin" user
:password "" ; default password => empty string
}
}
{:db-spec {:dbtype "h2" :dbname "example"}}
```

`config.clj`
Expand All @@ -35,19 +27,10 @@ add config
`db.clj`
```clj
(defrecord Database [datasource config]
component/Lifecycle
Lifecycle
(start [this]
(let [db-spec (get-in config [:config :db-spec])
cpds (doto (ComboPooledDataSource.)
(.setDriverClass (:classname db-spec))
(.setJdbcUrl (str "jdbc:" (:subprotocol db-spec) ":" (:subname db-spec)))
(.setUser (:user db-spec))
(.setPassword (:password db-spec))
;; expire excess connections after 30 minutes of inactivity:
(.setMaxIdleTimeExcessConnections (* 30 60))
;; expire connections after 3 hours of inactivity:
(.setMaxIdleTime (* 3 60 60)))]
(assoc this :datasource cpds)))
(let [db-spec (get-in config [:config :db-spec])]
(assoc this :datasource (connection/->pool HikariDataSource db-spec))))
(stop [this]
(.close datasource)
(assoc this :datasource nil)))
Expand All @@ -70,4 +53,3 @@ system
:app [:db]
:db [:config]})))
```

2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
[metosin/muuntaja "0.6.4"]
[metosin/ring-http-response "0.9.1"]
; config
[aero "1.1.3"]
[aero "1.1.6"]
; db
[com.github.seancorfield/next.jdbc "1.3.909"]
[com.h2database/h2 "2.2.224"]
Expand Down

0 comments on commit 81eaa0f

Please sign in to comment.