Remove CVS $Id$ keyword
[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 ;;;; This file, part of CLSQL, is Copyright (c) 2002-2010 by Kevin M. Rosenberg
11 ;;;;
12 ;;;; CLSQL users are granted the rights to distribute and use this software
13 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
14 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
15 ;;;; *************************************************************************
16
17 (in-package #:clsql-sys)
18
19 (defvar *db-pool* (make-hash-table :test #'equal))
20 (defvar *db-pool-lock* (make-process-lock "DB Pool lock"))
21
22 (defclass conn-pool ()
23   ((connection-spec :accessor connection-spec :initarg :connection-spec)
24    (database-type :accessor pool-database-type :initarg :pool-database-type)
25    (free-connections :accessor free-connections
26                      :initform (make-array 5 :fill-pointer 0 :adjustable t))
27    (all-connections :accessor all-connections
28                     :initform (make-array 5 :fill-pointer 0 :adjustable t))
29    (lock :accessor conn-pool-lock
30          :initform (make-process-lock "Connection pool"))))
31
32 (defun acquire-from-conn-pool (pool)
33   (or (with-process-lock ((conn-pool-lock pool) "Acquire from pool")
34         (when (plusp (length (free-connections pool)))
35           (let ((pconn (vector-pop (free-connections pool))))
36             ;; test if connection still valid.
37             ;; Currently, on supported on MySQL
38             (cond
39               ((eq :mysql (database-type pconn))
40                (handler-case
41                    (database-query "SHOW ERRORS LIMIT 1" pconn nil nil)
42                  (error (e)
43                    ;; we could check for error type 2006 for "SERVER GONE AWAY",
44                    ;; but, it's safer just to disconnect the pooled conn for any error
45                    (warn "Database connection ~S had an error when attempted to be acquired from the pool:
46   ~S
47 Disconnecting.~%"
48                          pconn e)
49                    (ignore-errors (database-disconnect pconn))
50                    nil)
51                  (:no-error (res fields)
52                    (declare (ignore res fields))
53                    pconn)))
54               (t
55                pconn)))))
56       (let ((conn (connect (connection-spec pool)
57                            :database-type (pool-database-type pool)
58                            :if-exists :new
59                            :make-default nil)))
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 may error if remote side closed connection
75           (ignore-errors (disconnect :database conn)))
76     (setf (fill-pointer (free-connections pool)) 0)
77     (setf (fill-pointer (all-connections pool)) 0))
78   nil)
79
80 (defun find-or-create-connection-pool (connection-spec database-type)
81   "Find connection pool in hash table, creates a new connection pool
82 if not found"
83   (with-process-lock (*db-pool-lock* "Find-or-create connection")
84     (let* ((key (list connection-spec database-type))
85            (conn-pool (gethash key *db-pool*)))
86       (unless conn-pool
87         (setq conn-pool (make-instance 'conn-pool
88                                        :connection-spec connection-spec
89                                        :pool-database-type database-type))
90         (setf (gethash key *db-pool*) conn-pool))
91       conn-pool)))
92
93 (defun acquire-from-pool (connection-spec database-type &optional pool)
94   (unless (typep pool 'conn-pool)
95     (setf pool (find-or-create-connection-pool connection-spec database-type)))
96   (acquire-from-conn-pool pool))
97
98 (defun release-to-pool (database)
99   (release-to-conn-pool database))
100
101 (defun disconnect-pooled (&optional clear)
102   "Disconnects all connections in the pool."
103   (with-process-lock (*db-pool-lock* "Disconnect pooled")
104     (maphash
105      #'(lambda (key conn-pool)
106          (declare (ignore key))
107          (clear-conn-pool conn-pool))
108      *db-pool*)
109     (when clear (clrhash *db-pool*)))
110   t)
111
112 ;(defun pool-start-sql-recording (pool &key (types :command))
113 ;  "Start all stream in the pool recording actions of TYPES"
114 ;  (dolist (con (pool-connections pool))
115 ;    (start-sql-recording :type types
116 ;                        :database (connection-database con))))
117
118 ;(defun pool-stop-sql-recording (pool &key (types :command))
119 ;  "Start all stream in the pool recording actions of TYPES"
120 ;  (dolist (con (pool-connections pool))
121 ;    (stop-sql-recording :type types
122 ;                         :database (connection-database con))))
123
124 ;(defmacro with-database-connection (pool &body body)
125 ;  `(let ((connection (obtain-connection ,pool))
126 ;         (results nil))
127 ;    (unwind-protect
128 ;         (with-database ((connection-database connection))
129 ;           (setq results (multiple-value-list (progn ,@body))))
130 ;      (release-connection connection))
131 ;    (values-list results)))