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.2 2002/10/21 07:45:50 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)
23 (defun make-lock (name) (declare (ignore name)) nil)
26 (defmacro with-lock-held ((lock desc) &body body)
27 (declare (ignore lock desc))
31 (defvar *db-pool* (make-hash-table :test #'equal))
32 (defvar *db-pool-lock* (make-lock "DB Pool lock"))
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"))))
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)
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))
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)))))
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))
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*)))
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))
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))
87 (defun release-to-pool (database)
88 (release-to-conn-pool database))
90 (defun disconnect-pooled (&optional clear)
91 "Disconnects all connections in the pool"
92 (with-lock-held (*db-pool-lock* "Find connection")
94 #'(lambda (key conn-pool)
95 (declare (ignore key))
96 (clear-conn-pool conn-pool))
98 (when clear (clrhash *db-pool*)))