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 (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)
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)
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))
40 (defvar *db-pool* (make-hash-table :test #'equal))
41 (defvar *db-pool-lock* (make-process-lock "DB Pool lock"))
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"))))
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)
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))
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)))))
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))
79 (defun find-or-create-connection-pool (connection-spec database-type)
80 "Find connection pool in hash table, creates a new connection pool
82 (with-process-lock (*db-pool-lock* "Find-or-create 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* "Disconnect pooled")
104 #'(lambda (key conn-pool)
105 (declare (ignore key))
106 (clear-conn-pool conn-pool))
108 (when clear (clrhash *db-pool*)))
111 ;(defun pool-start-sql-recording (pool &key (types :command))
112 ; "Start all stream in the pool recording actions of TYPES"
113 ; (dolist (con (pool-connections pool))
114 ; (start-sql-recording :type types
115 ; :database (connection-database con))))
117 ;(defun pool-stop-sql-recording (pool &key (types :command))
118 ; "Start all stream in the pool recording actions of TYPES"
119 ; (dolist (con (pool-connections pool))
120 ; (stop-sql-recording :type types
121 ; :database (connection-database con))))
123 ;(defmacro with-database-connection (pool &body body)
124 ; `(let ((connection (obtain-connection ,pool))
127 ; (with-database ((connection-database connection))
128 ; (setq results (multiple-value-list (progn ,@body))))
129 ; (release-connection connection))
130 ; (values-list results)))