r10547: fix warning
[clsql.git] / sql / 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$
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-sys)
20
21 (defvar *db-pool* (make-hash-table :test #'equal))
22 (defvar *db-pool-lock* (make-process-lock "DB Pool lock"))
23
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"))))
33
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)
40                            :if-exists :new)))
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))
44         conn)))
45
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)))))
50
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 may error if remote side closed connection
56           (ignore-errors (disconnect :database conn)))
57     (setf (fill-pointer (free-connections pool)) 0)
58     (setf (fill-pointer (all-connections pool)) 0))
59   nil)
60
61 (defun find-or-create-connection-pool (connection-spec database-type)
62   "Find connection pool in hash table, creates a new connection pool
63 if not found"
64   (with-process-lock (*db-pool-lock* "Find-or-create connection")
65     (let* ((key (list connection-spec database-type))
66            (conn-pool (gethash key *db-pool*)))
67       (unless conn-pool
68         (setq conn-pool (make-instance 'conn-pool
69                                        :connection-spec connection-spec
70                                        :pool-database-type database-type))
71         (setf (gethash key *db-pool*) conn-pool))
72       conn-pool)))
73
74 (defun acquire-from-pool (connection-spec database-type &optional pool)
75   (unless (typep pool 'conn-pool)
76     (setf pool (find-or-create-connection-pool connection-spec database-type)))
77   (acquire-from-conn-pool pool))
78
79 (defun release-to-pool (database)
80   (release-to-conn-pool database))
81
82 (defun disconnect-pooled (&optional clear)
83   "Disconnects all connections in the pool."
84   (with-process-lock (*db-pool-lock* "Disconnect pooled")
85     (maphash
86      #'(lambda (key conn-pool)
87          (declare (ignore key))
88          (clear-conn-pool conn-pool))
89      *db-pool*)
90     (when clear (clrhash *db-pool*)))
91   t)
92
93 ;(defun pool-start-sql-recording (pool &key (types :command))
94 ;  "Start all stream in the pool recording actions of TYPES"
95 ;  (dolist (con (pool-connections pool))
96 ;    (start-sql-recording :type types
97 ;                        :database (connection-database con))))
98
99 ;(defun pool-stop-sql-recording (pool &key (types :command))
100 ;  "Start all stream in the pool recording actions of TYPES"
101 ;  (dolist (con (pool-connections pool))
102 ;    (stop-sql-recording :type types
103 ;                         :database (connection-database con))))
104
105 ;(defmacro with-database-connection (pool &body body)
106 ;  `(let ((connection (obtain-connection ,pool))
107 ;         (results nil))
108 ;    (unwind-protect
109 ;         (with-database ((connection-database connection))
110 ;           (setq results (multiple-value-list (progn ,@body))))
111 ;      (release-connection connection))
112 ;    (values-list results)))