r3129: dtc patches
[clsql.git] / sql / pool.lisp
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.lisp,v 1.2 2002/10/21 07:45:50 kevin 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 #-scl
23 (defun make-lock (name) (declare (ignore name)) nil)
24
25 #-scl
26 (defmacro with-lock-held ((lock desc) &body body)
27   (declare (ignore lock desc))
28   `(progn
29      ,@body))
30
31 (defvar *db-pool* (make-hash-table :test #'equal))
32 (defvar *db-pool-lock* (make-lock "DB Pool lock"))
33
34 (defclass conn-pool ()
35   ((connection-spec :accessor connection-spec :initarg :connection-spec)
36    (database-type :accessor database-type :initarg :database-type)
37    (free-connections :accessor free-connections
38                      :initform (make-array 5 :fill-pointer 0 :adjustable t))
39    (all-connections :accessor all-connections
40                     :initform (make-array 5 :fill-pointer 0 :adjustable t))
41    (lock :accessor conn-pool-lock
42          :initform (make-lock "Connection pool"))))
43
44 (defun acquire-from-conn-pool (pool)
45   (or (with-lock-held ((conn-pool-lock pool) "Acquire from pool")
46         (and (plusp (length (free-connections pool)))
47              (vector-pop (free-connections pool))))
48       (let ((conn (connect (connection-spec pool)
49                            :database-type (database-type pool)
50                            :if-exists :new)))
51         (with-lock-held ((conn-pool-lock pool) "Acquire from pool")
52           (vector-push-extend conn (all-connections pool))
53           (setf (conn-pool conn) pool))
54         conn)))
55
56 (defun release-to-conn-pool (conn)
57   (let ((pool (conn-pool conn)))
58     (with-lock-held ((conn-pool-lock pool) "Release to pool")
59       (vector-push-extend conn (free-connections pool)))))
60
61 (defun clear-conn-pool (pool)
62   (with-lock-held ((conn-pool-lock pool) "Clear pool")
63     (loop for conn across (all-connections pool)
64           do (setf (conn-pool conn) nil)
65           (disconnect :database conn))
66     (setf (fill-pointer (free-connections pool)) 0)
67     (setf (fill-pointer (all-connections pool)) 0))
68   nil)
69
70 (defun find-or-create-connection-pool (connection-spec database-type)
71   "Find connection pool in hash table, creates a new connection pool if not found"
72   (with-lock-held (*db-pool-lock* "Find connection")
73     (let* ((key (list connection-spec database-type))
74            (conn-pool (gethash key *db-pool*)))
75       (unless conn-pool
76         (setq conn-pool (make-instance 'conn-pool
77                                        :connection-spec connection-spec
78                                        :database-type database-type))
79         (setf (gethash key *db-pool*) conn-pool))
80       conn-pool)))
81
82 (defun acquire-from-pool (connection-spec database-type &optional pool)
83   (unless (typep pool 'conn-pool)
84     (setf pool (find-or-create-connection-pool connection-spec database-type)))
85   (acquire-from-conn-pool pool))
86
87 (defun release-to-pool (database)
88   (release-to-conn-pool database))
89
90 (defun disconnect-pooled (&optional clear)
91   "Disconnects all connections in the pool"
92   (with-lock-held (*db-pool-lock* "Find connection")
93     (maphash
94      #'(lambda (key conn-pool)
95          (declare (ignore key))
96          (clear-conn-pool conn-pool))
97      *db-pool*)
98     (when clear (clrhash *db-pool*)))
99   t)
100