Awesome
ring-jdbc-session
A Clojure library to implement an HTTP session store for Ring using a JDBC backing store.
Tested with Clojure versions 1.3, 1.4 and 1.5-RC3 with the following databases:
- H2 in-memory database
- MySQL
- PostgreSQL
Usage
Leiningen dependency
Coordinates: [ring-jdbc-session "0.1.0"]
On Clojars: https://clojars.org/ring-jdbc-session
Pre-requisite
-
First of all, you need a connection-pooling
javax.sql.DataSource
instance for use with this library. ADataSource
is like a connection factory. See Clj-DBCP (example below uses this) and c3p0 on how to create aDataSource
. Let's assume theDataSource
is bound to a vards
.(def ds (clj-dbcp.core/make-datasource :mysql {:host "localhost" :database "abc" :user "dbuser" :password "s3cr3t"}))
-
Create the session table if not already created:
(ring-jdbc-session.core/create-session-table ds)
Or simply create a table using the following DDL:
CREATE TABLE ring_session (session_key VARCHAR(100) UNIQUE NOT NULL, session_val VARCHAR(1024), session_ts TIMESTAMP NOT NULL)
Create and use the session store
Use with Ring
(let [jdbc-ss (ring-jdbc-session.core/make-session-store ds)]
(ring-jdbc-session.core/start-cleaner jdbc-ss) ; starting cleaner is optional
(ring.middleware.session/wrap-session handler {:store jdbc-ss}))
Use with Compojure
(let [jdbc-ss (ring-jdbc-session.core/make-session-store ds)]
(ring-jdbc-session.core/start-cleaner jdbc-ss) ; starting cleaner is optional
(compojure.handler/site handler {:session {:store jdbc-ss}}))
Documentation
See file doc/intro.md
in this repo.
Getting in touch
On Twitter: @kumarshantanu
License
Copyright © 2013 Shantanu Kumar
Distributed under the Eclipse Public License, the same as Clojure.