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 #+cmu (mp:make-lock name)
24 #+lispworks (mp:make-lock :name name)
25 #+openmcl (ccl:make-lock :name name)
26 #+sb-thread (sb-thread:make-mutex :name name)
27 #+scl (thread:make-lock name)
28 #-(or allegro cmu lispworks openmcl sb-thread scl) (declare (ignore name))
29 #-(or allegro cmu lispworks openmcl sb-thread scl) nil)
31 (defmacro with-process-lock ((lock desc) &body body)
32 #+(or cmu allegro lispworks openmcl sb-thread)
33 (declare (ignore desc))
34 #+(or allegro cmu lispworks openmcl sb-thread)
37 #+allegro (mp:with-process-lock (,l) ,@body)
38 #+cmu `(mp:with-lock-held (,lock) ,@body)
39 #+openmcl (ccl:with-lock-grabbed (,lock) ,@body)
40 #+lispworks (mp:with-lock (,l) ,@body)
41 #+sb-thread (sb-thread:with-recursive-lock (,lock) ,@body)
44 #+scl `(thread:with-lock-held (,lock ,desc) ,@body)
46 #-(or cmu allegro lispworks openmcl sb-thread scl) (declare (ignore lock desc))
47 #-(or cmu allegro lispworks openmcl sb-thread scl) `(progn ,@body))
49 (defvar *db-pool* (make-hash-table :test #'equal))
50 (defvar *db-pool-lock* (make-process-lock "DB Pool lock"))
52 (defclass conn-pool ()
53 ((connection-spec :accessor connection-spec :initarg :connection-spec)
54 (database-type :accessor pool-database-type :initarg :pool-database-type)
55 (free-connections :accessor free-connections
56 :initform (make-array 5 :fill-pointer 0 :adjustable t))
57 (all-connections :accessor all-connections
58 :initform (make-array 5 :fill-pointer 0 :adjustable t))
59 (lock :accessor conn-pool-lock
60 :initform (make-process-lock "Connection pool"))))
62 (defun acquire-from-conn-pool (pool)
63 (or (with-process-lock ((conn-pool-lock pool) "Acquire from pool")
64 (and (plusp (length (free-connections pool)))
65 (vector-pop (free-connections pool))))
66 (let ((conn (connect (connection-spec pool)
67 :database-type (pool-database-type pool)
69 (with-process-lock ((conn-pool-lock pool) "Acquire from pool")
70 (vector-push-extend conn (all-connections pool))
71 (setf (conn-pool conn) pool))
74 (defun release-to-conn-pool (conn)
75 (let ((pool (conn-pool conn)))
76 (with-process-lock ((conn-pool-lock pool) "Release to pool")
77 (vector-push-extend conn (free-connections pool)))))
79 (defun clear-conn-pool (pool)
80 (with-process-lock ((conn-pool-lock pool) "Clear pool")
81 (loop for conn across (all-connections pool)
82 do (setf (conn-pool conn) nil)
83 (disconnect :database conn))
84 (setf (fill-pointer (free-connections pool)) 0)
85 (setf (fill-pointer (all-connections pool)) 0))
88 (defun find-or-create-connection-pool (connection-spec database-type)
89 "Find connection pool in hash table, creates a new connection pool
91 (with-process-lock (*db-pool-lock* "Find-or-create connection")
92 (let* ((key (list connection-spec database-type))
93 (conn-pool (gethash key *db-pool*)))
95 (setq conn-pool (make-instance 'conn-pool
96 :connection-spec connection-spec
97 :pool-database-type database-type))
98 (setf (gethash key *db-pool*) conn-pool))
101 (defun acquire-from-pool (connection-spec database-type &optional pool)
102 (unless (typep pool 'conn-pool)
103 (setf pool (find-or-create-connection-pool connection-spec database-type)))
104 (acquire-from-conn-pool pool))
106 (defun release-to-pool (database)
107 (release-to-conn-pool database))
109 (defun disconnect-pooled (&optional clear)
110 "Disconnects all connections in the pool."
111 (with-process-lock (*db-pool-lock* "Disconnect pooled")
113 #'(lambda (key conn-pool)
114 (declare (ignore key))
115 (clear-conn-pool conn-pool))
117 (when clear (clrhash *db-pool*)))
120 ;(defun pool-start-sql-recording (pool &key (types :command))
121 ; "Start all stream in the pool recording actions of TYPES"
122 ; (dolist (con (pool-connections pool))
123 ; (start-sql-recording :type types
124 ; :database (connection-database con))))
126 ;(defun pool-stop-sql-recording (pool &key (types :command))
127 ; "Start all stream in the pool recording actions of TYPES"
128 ; (dolist (con (pool-connections pool))
129 ; (stop-sql-recording :type types
130 ; :database (connection-database con))))
132 ;(defmacro with-database-connection (pool &body body)
133 ; `(let ((connection (obtain-connection ,pool))
136 ; (with-database ((connection-database connection))
137 ; (setq results (multiple-value-list (progn ,@body))))
138 ; (release-connection connection))
139 ; (values-list results)))