r8821: integrate usql support
[clsql.git] / base / pool.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          pool.lisp
6 ;;;; Purpose:       Support function for connection pool
7 ;;;; Programmers:   Kevin M. Rosenberg, Marc Battyani
8 ;;;; Date Started:  Apr 2002
9 ;;;;
10 ;;;; $Id: pool.lisp 7061 2003-09-07 06:34:45Z kevin $
11 ;;;;
12 ;;;; This file, part of CLSQL, is Copyright (c) 2002-2003 by Kevin M. Rosenberg
13 ;;;;
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 ;;;; *************************************************************************
18
19 (in-package #:clsql-base-sys)
20
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)
27
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)
33   (let ((l (gensym)))
34     `(let ((,l ,lock))
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))
39
40 (defvar *db-pool* (make-hash-table :test #'equal))
41 (defvar *db-pool-lock* (make-process-lock "DB Pool lock"))
42
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"))))
52
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)
59                            :if-exists :new)))
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))
63         conn)))
64
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)))))
69
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))
77   nil)
78
79 (defun find-or-create-connection-pool (connection-spec database-type)
80   "Find connection pool in hash table, creates a new connection pool
81 if not found"
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*)))
85       (unless conn-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))
90       conn-pool)))
91
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))
96
97 (defun release-to-pool (database)
98   (release-to-conn-pool database))
99
100 (defun disconnect-pooled (&optional clear)
101   "Disconnects all connections in the pool."
102   (with-process-lock (*db-pool-lock* "Disconnect pooled")
103     (maphash
104      #'(lambda (key conn-pool)
105          (declare (ignore key))
106          (clear-conn-pool conn-pool))
107      *db-pool*)
108     (when clear (clrhash *db-pool*)))
109   t)
110
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))))
116
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))))
122
123 ;(defmacro with-database-connection (pool &body body)
124 ;  `(let ((connection (obtain-connection ,pool))
125 ;         (results nil))
126 ;    (unwind-protect
127 ;         (with-database ((connection-database connection))
128 ;           (setq results (multiple-value-list (progn ,@body))))
129 ;      (release-connection connection))
130 ;    (values-list results)))