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
12 ;;;; This file, part of CLSQL, is Copyright (c) 2002-2003 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 (in-package #:clsql-base-sys)
21 (defvar *db-pool* (make-hash-table :test #'equal))
22 (defvar *db-pool-lock* (make-process-lock "DB Pool lock"))
24 (defclass conn-pool ()
25 ((connection-spec :accessor connection-spec :initarg :connection-spec)
26 (database-type :accessor pool-database-type :initarg :pool-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 (lock :accessor conn-pool-lock
32 :initform (make-process-lock "Connection pool"))))
34 (defun acquire-from-conn-pool (pool)
35 (or (with-process-lock ((conn-pool-lock pool) "Acquire from pool")
36 (and (plusp (length (free-connections pool)))
37 (vector-pop (free-connections pool))))
38 (let ((conn (connect (connection-spec pool)
39 :database-type (pool-database-type pool)
41 (with-process-lock ((conn-pool-lock pool) "Acquire from pool")
42 (vector-push-extend conn (all-connections pool))
43 (setf (conn-pool conn) pool))
46 (defun release-to-conn-pool (conn)
47 (let ((pool (conn-pool conn)))
48 (with-process-lock ((conn-pool-lock pool) "Release to pool")
49 (vector-push-extend conn (free-connections pool)))))
51 (defun clear-conn-pool (pool)
52 (with-process-lock ((conn-pool-lock pool) "Clear pool")
53 (loop for conn across (all-connections pool)
54 do (setf (conn-pool conn) nil)
55 (disconnect :database conn))
56 (setf (fill-pointer (free-connections pool)) 0)
57 (setf (fill-pointer (all-connections pool)) 0))
60 (defun find-or-create-connection-pool (connection-spec database-type)
61 "Find connection pool in hash table, creates a new connection pool
63 (with-process-lock (*db-pool-lock* "Find-or-create connection")
64 (let* ((key (list connection-spec database-type))
65 (conn-pool (gethash key *db-pool*)))
67 (setq conn-pool (make-instance 'conn-pool
68 :connection-spec connection-spec
69 :pool-database-type database-type))
70 (setf (gethash key *db-pool*) conn-pool))
73 (defun acquire-from-pool (connection-spec database-type &optional pool)
74 (unless (typep pool 'conn-pool)
75 (setf pool (find-or-create-connection-pool connection-spec database-type)))
76 (acquire-from-conn-pool pool))
78 (defun release-to-pool (database)
79 (release-to-conn-pool database))
81 (defun disconnect-pooled (&optional clear)
82 "Disconnects all connections in the pool."
83 (with-process-lock (*db-pool-lock* "Disconnect pooled")
85 #'(lambda (key conn-pool)
86 (declare (ignore key))
87 (clear-conn-pool conn-pool))
89 (when clear (clrhash *db-pool*)))
92 ;(defun pool-start-sql-recording (pool &key (types :command))
93 ; "Start all stream in the pool recording actions of TYPES"
94 ; (dolist (con (pool-connections pool))
95 ; (start-sql-recording :type types
96 ; :database (connection-database con))))
98 ;(defun pool-stop-sql-recording (pool &key (types :command))
99 ; "Start all stream in the pool recording actions of TYPES"
100 ; (dolist (con (pool-connections pool))
101 ; (stop-sql-recording :type types
102 ; :database (connection-database con))))
104 ;(defmacro with-database-connection (pool &body body)
105 ; `(let ((connection (obtain-connection ,pool))
108 ; (with-database ((connection-database connection))
109 ; (setq results (multiple-value-list (progn ,@body))))
110 ; (release-connection connection))
111 ; (values-list results)))