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