r7061: initial property settings
[clsql.git] / sql / pool.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          pool.lisp
6 ;;;; Purpose:       Support function for connection pool
7 ;;;; Programmers:   Kevin M. Rosenberg, Marc Battyani
8 ;;;; Date Started:  Apr 2002
9 ;;;;
10 ;;;; $Id$
11 ;;;;
12 ;;;; This file, part of CLSQL, is Copyright (c) 2002-2003 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 (in-package clsql-sys)
20
21 (defun make-process-lock (name) 
22   #+allegro (mp:make-process-lock :name name)
23   #+scl (thread:make-lock name)
24   #+lispworks (mp:make-lock :name name)
25   #-(or allegro scl lispworks) (declare (ignore name))
26   #-(or allegro scl lispworks) nil)
27
28 (defmacro with-process-lock ((lock desc) &body body)
29   #+scl `(thread:with-lock-held (,lock ,desc) ,@body)
30   #+(or allegro lispworks)
31   (declare (ignore desc))
32   #+(or allegro lispworks)
33   (let ((l (gensym)))
34     `(let ((,l ,lock))
35        #+allegro (mp:with-process-lock (,l) ,@body)
36        #+lispworks (mp:with-lock (,l) ,@body)))
37   #-(or scl allegro lispworks) (declare (ignore lock desc))
38   #-(or scl allegro lispworks) `(progn ,@body))
39
40 (defvar *db-pool* (make-hash-table :test #'equal))
41 (defvar *db-pool-lock* (make-process-lock "DB Pool lock"))
42
43 (defclass conn-pool ()
44   ((connection-spec :accessor connection-spec :initarg :connection-spec)
45    (database-type :accessor database-type :initarg :database-type)
46    (free-connections :accessor free-connections
47                      :initform (make-array 5 :fill-pointer 0 :adjustable t))
48    (all-connections :accessor all-connections
49                     :initform (make-array 5 :fill-pointer 0 :adjustable t))
50    (lock :accessor conn-pool-lock
51          :initform (make-process-lock "Connection pool"))))
52
53 (defun acquire-from-conn-pool (pool)
54   (or (with-process-lock ((conn-pool-lock pool) "Acquire from pool")
55         (and (plusp (length (free-connections pool)))
56              (vector-pop (free-connections pool))))
57       (let ((conn (connect (connection-spec pool)
58                            :database-type (database-type pool)
59                            :if-exists :new)))
60         (with-process-lock ((conn-pool-lock pool) "Acquire from pool")
61           (vector-push-extend conn (all-connections pool))
62           (setf (conn-pool conn) pool))
63         conn)))
64
65 (defun release-to-conn-pool (conn)
66   (let ((pool (conn-pool conn)))
67     (with-process-lock ((conn-pool-lock pool) "Release to pool")
68       (vector-push-extend conn (free-connections pool)))))
69
70 (defun clear-conn-pool (pool)
71   (with-process-lock ((conn-pool-lock pool) "Clear pool")
72     (loop for conn across (all-connections pool)
73           do (setf (conn-pool conn) nil)
74           (disconnect :database conn))
75     (setf (fill-pointer (free-connections pool)) 0)
76     (setf (fill-pointer (all-connections pool)) 0))
77   nil)
78
79 (defun find-or-create-connection-pool (connection-spec database-type)
80   "Find connection pool in hash table, creates a new connection pool if not found"
81   (with-process-lock (*db-pool-lock* "Find-or-create connection")
82     (let* ((key (list connection-spec database-type))
83            (conn-pool (gethash key *db-pool*)))
84       (unless conn-pool
85         (setq conn-pool (make-instance 'conn-pool
86                                        :connection-spec connection-spec
87                                        :database-type database-type))
88         (setf (gethash key *db-pool*) conn-pool))
89       conn-pool)))
90
91 (defun acquire-from-pool (connection-spec database-type &optional pool)
92   (unless (typep pool 'conn-pool)
93     (setf pool (find-or-create-connection-pool connection-spec database-type)))
94   (acquire-from-conn-pool pool))
95
96 (defun release-to-pool (database)
97   (release-to-conn-pool database))
98
99 (defun disconnect-pooled (&optional clear)
100   "Disconnects all connections in the pool"
101   (with-process-lock (*db-pool-lock* "Disconnect pooled")
102     (maphash
103      #'(lambda (key conn-pool)
104          (declare (ignore key))
105          (clear-conn-pool conn-pool))
106      *db-pool*)
107     (when clear (clrhash *db-pool*)))
108   t)