Back

component (clj)

(source)

function

(component clazz db-spec) (component clazz db-spec close-fn)
Takes the same arguments as `->pool` but returns an entity compatible with Stuart Sierra's Component: when `com.stuartsierra.component/start` is called on it, it builds a connection pooled datasource, and returns an entity that can either be invoked as a function with no arguments to return that datasource, or can have `com.stuartsierra.component/stop` called on it to shutdown the datasource (and return a new startable entity). If `db-spec` contains `:init-fn`, that is assumed to be a function that should be called on the newly-created datasource. This allows for modification of (mutable) connection pooled datasource and/or some sort of database initialization/setup to be called automatically. By default, the datasource is shutdown by calling `.close` on it. If the datasource class implements `java.io.Closeable` then a direct, type-hinted call to `.close` will be used, with no reflection, otherwise Java reflection will be used to find the first `.close` method in the datasource class that takes no arguments and returns `void`. If neither of those behaviors is appropriate, you may supply a third argument to this function -- `close-fn` -- which performs whatever action is appropriate to your chosen datasource class.

Examples