r1857: Completed connection pool.
[clsql.git] / sql / pool.cl
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          pool.cl
6 ;;;; Purpose:       Support function for connection pool
7 ;;;; Programmers:   Kevin M. Rosenberg, Marc Battyani
8 ;;;; Date Started:  Apr 2002
9 ;;;;
10 ;;;; $Id: pool.cl,v 1.3 2002/05/01 20:22:16 marc.battyani Exp $
11 ;;;;
12 ;;;; This file, part of CLSQL, is Copyright (c) 2002 by Kevin M. Rosenberg
13 ;;;;
14 ;;;; CLSQL users are granted the rights to distribute and use this software
15 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
16 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
17 ;;;; *************************************************************************
18
19 (declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
20 (in-package :clsql-sys)
21
22 (defvar *db-pool* (make-hash-table :test #'equal))
23
24 (defclass conn-pool ()
25   ((connection-spec :accessor connection-spec :initarg :connection-spec)
26    (database-type :accessor database-type :initarg :database-type)
27    (free-connections :accessor free-connections
28                      :initform (make-array 5 :fill-pointer 0 :adjustable t))
29    (all-connections :accessor all-connections
30                     :initform (make-array 5 :fill-pointer 0 :adjustable t))))
31
32 (defun acquire-from-conn-pool (pool)
33   (if (zerop (length (free-connections pool)))
34     (let ((conn (connect (connection-spec pool)
35                          :database-type (database-type pool) :if-exists :new)))
36       (vector-push-extend conn (all-connections pool))
37       (setf (conn-pool conn) pool)
38       conn)
39     (vector-pop (free-connections pool))))
40
41 (defun release-to-conn-pool (conn)
42   (vector-push-extend conn (free-connections (conn-pool conn))))
43
44 (defun clear-conn-pool (pool)
45   (loop for conn across (all-connections pool)
46         do (disconnect :database conn))
47   (setf (fill-pointer (free-connections pool)) 0)
48   (setf (fill-pointer (all-connections pool)) 0))
49
50 (defun find-or-create-conn-pool (connection-spec database-type)
51   "Find connection vector in hash table, creates a new conn-vector if not found"
52   (let* ((key (list connection-spec database-type))
53          (conn-pool (gethash key *db-pool*)))
54     (unless conn-pool
55       (setq conn-pool (make-instance 'conn-pool
56                                      :connection-spec connection-spec
57                                      :database-type database-type))
58       (setf (gethash key *db-pool*) conn-pool))
59     conn-pool))
60
61 (defun acquire-from-pool (connection-spec database-type &optional pool)
62   (unless pool (setf pool (find-or-create-conn-pool connection-spec database-type)))
63   (acquire-from-conn-pool pool))
64
65 (defun release-to-pool (database)
66   (release-to-conn-pool database))
67
68 (defun disconnect-pooled (&optional clear)
69   "Disconnects all connections in the pool"
70   (maphash
71    #'(lambda (key conn-pool)
72        (declare (ignore key))
73        (clear-conn-pool conn-pool))
74    *db-pool*)
75   (when clear (clrhash *db-pool*))
76   t)
77
78 ;;; with-db-from-pool is the macro you should use if you want to use pooled connections.
79 ;;; You can use it with a connection spec and database type or directly with a conn-pool.
80 ;;; When you give a conn-pool the connection spec and database type are ignored
81
82 (defmacro with-db-from-pool ((db-var connection-spec database-type &optional conn-pool) &body body)
83   "Evaluate the body in an environment, where `db-var' is bound to a
84 database connection acquired from the connection pool
85 The connection is automatically released to the connection pool on exit from the body.
86 If a pool is given then the connection-spec database-type are ignored."
87   `(let ((,db-var (acquire-from-pool ,connection-spec ,database-type ,conn-pool)))
88      (unwind-protect
89           (let ((,db-var ,db-var)) ,@body)
90        (release-to-pool ,db-var))))