Automated commit for debian release 6.7.2-1
[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 (defparameter *db-pool-max-free-connections* 4
20   "Threshold of free-connections in the pool before we disconnect a database
21   rather than returning it to the pool.  NIL for no limit.  This is really a
22   heuristic that should, on avg keep the free connections about this size.")
23
24 (defvar *db-pool* (make-hash-table :test #'equal))
25 (defvar *db-pool-lock* (make-process-lock "DB Pool lock"))
26
27 (defclass conn-pool ()
28   ((connection-spec :accessor connection-spec :initarg :connection-spec)
29    (database-type :accessor pool-database-type :initarg :pool-database-type)
30    (free-connections :accessor free-connections :initform nil)
31    (all-connections :accessor all-connections :initform nil)
32    (lock :accessor conn-pool-lock
33          :initform (make-process-lock "Connection pool"))))
34
35
36
37
38 (defun acquire-from-pool (connection-spec database-type &optional pool (encoding *default-encoding*))
39   "Try to find a working database connection in the pool or create a new
40 one if needed. This performs 1 query against the DB to ensure it's still
41 valid. When possible (postgres, mssql) that query will be a reset
42 command to put the connection back into its default state."
43   (unless (typep pool 'conn-pool)
44     (setf pool (find-or-create-connection-pool connection-spec database-type)))
45   (or
46    (loop for pconn = (with-process-lock ((conn-pool-lock pool) "Acquire")
47                        (pop (free-connections pool)))
48          always pconn
49          thereis
50          ;; test if connection still valid.
51          ;; (e.g. db reboot -> invalid connection )
52          (handler-case
53              (progn (database-acquire-from-conn-pool pconn)
54                     pconn)
55            (sql-database-error (e)
56              ;; we could check for a specific error,
57              ;; but, it's safer just to disconnect the pooled conn for any error ?
58              (warn "Database connection ~S had an error while acquiring from the pool:
59   ~S
60 Disconnecting.~%"
61                    pconn e)
62              ;;run database disconnect to give chance for cleanup
63              ;;there, then remove it from the lists of connected
64              ;;databases.
65              (%pool-force-disconnect pconn)
66              (with-process-lock ((conn-pool-lock pool) "remove dead conn")
67                (setf (all-connections pool)
68                      (delete pconn (all-connections pool))))
69              nil)))
70    (let ((conn (connect (connection-spec pool)
71                         :database-type (pool-database-type pool)
72                         :if-exists :new
73                         :make-default nil
74                         :encoding encoding)))
75      (setf (conn-pool conn) pool)
76      (with-process-lock ((conn-pool-lock pool) "new conection")
77        (push conn (all-connections pool)))
78      conn)))
79
80 (defun release-to-pool (database &optional (pool (conn-pool database)))
81   "Release a database connection to the pool. The backend will have a
82 chance to do cleanup."
83   (unless (conn-pool database) (setf (conn-pool database) pool))
84   (cond
85     ;;We read the list of free-connections outside the lock. This
86     ;;should be fine as long as that list is never dealt with
87     ;;destructively (push and pop destructively modify the place,
88     ;;not the list). Multiple threads getting to this test at the
89     ;;same time might result in the free-connections getting
90     ;;longer... meh.
91     ((or (and *db-pool-max-free-connections*
92               (>= (length (free-connections pool))
93                   *db-pool-max-free-connections*)))
94      (%pool-force-disconnect database)
95
96      (with-process-lock ((conn-pool-lock pool) "Remove extra Conn")
97        (setf (all-connections pool)
98              (delete database (all-connections pool)))))
99     (t
100      ;;let it do cleanup
101      (database-release-to-conn-pool database)
102      (with-process-lock ((conn-pool-lock pool) "Release to pool")
103        (push database (free-connections pool))))))
104
105 (defmethod database-acquire-from-conn-pool (database)
106   (case (database-underlying-type database)
107     (:postgresql
108        (database-execute-command "RESET ALL" database))
109     (:mysql
110        (database-query "SHOW ERRORS LIMIT 1" database nil nil))
111     (:mssql
112        ;; rpc escape sequence since this can't be called as a normal sp.
113        ;;http://msdn.microsoft.com/en-us/library/aa198358%28SQL.80%29.aspx
114        (database-execute-command "{rpc sp_reset_connection}" database))
115     (T
116        (database-query "SELECT 1;"  database '(integer) nil))))
117
118 (defmethod database-release-to-conn-pool (database)
119   (case (database-underlying-type database)
120     (:postgresql
121        (ignore-errors
122          ;;http://www.postgresql.org/docs/current/static/sql-discard.html
123          ;;this was introduced relatively recently, wrap in ignore-errors
124          ;;so that it doesn't choke older versions.
125          (database-execute-command "DISCARD ALL" database)))))
126
127 (defun clear-conn-pool (pool)
128   "Be careful this function will disconnect connections without regard
129 to whether another thread is actively using them."
130   (with-process-lock ((conn-pool-lock pool) "Clear pool")
131     (mapc #'%pool-force-disconnect (all-connections pool))
132     (setf (all-connections pool) nil
133           (free-connections pool) nil))
134   nil)
135
136 (defun find-or-create-connection-pool (connection-spec database-type)
137   "Find connection pool in hash table, creates a new connection pool
138 if not found"
139   (let ((key (list connection-spec database-type)))
140     (with-process-lock (*db-pool-lock* "Find-or-create connection")
141       (or (gethash key *db-pool*)
142           (setf (gethash key *db-pool*)
143                 (make-instance 'conn-pool
144                                :connection-spec connection-spec
145                                :pool-database-type database-type))))))
146
147 (defun disconnect-pooled (&optional clear)
148   "Disconnects all connections in the pool. When clear, also deletes
149 the pool objects."
150   (with-process-lock (*db-pool-lock* "Disconnect pooled")
151     (maphash
152      #'(lambda (key conn-pool)
153          (declare (ignore key))
154          (clear-conn-pool conn-pool))
155      *db-pool*)
156     (when clear (clrhash *db-pool*)))
157   t)
158
159 (defun %pool-force-disconnect (database)
160   "Force disconnection of a connection from the pool."
161   ;;so it isn't just returned to pool
162   (setf (conn-pool database) nil)
163   ;; disconnect may error if remote side closed connection
164   (ignore-errors (disconnect :database database)))
165
166 ;(defun pool-start-sql-recording (pool &key (types :command))
167 ;  "Start all stream in the pool recording actions of TYPES"
168 ;  (dolist (con (pool-connections pool))
169 ;    (start-sql-recording :type types
170 ;                        :database (connection-database con))))
171
172 ;(defun pool-stop-sql-recording (pool &key (types :command))
173 ;  "Start all stream in the pool recording actions of TYPES"
174 ;  (dolist (con (pool-connections pool))
175 ;    (stop-sql-recording :type types
176 ;                         :database (connection-database con))))
177
178 ;(defmacro with-database-connection (pool &body body)
179 ;  `(let ((connection (obtain-connection ,pool))
180 ;         (results nil))
181 ;    (unwind-protect
182 ;         (with-database ((connection-database connection))
183 ;           (setq results (multiple-value-list (progn ,@body))))
184 ;      (release-connection connection))
185 ;    (values-list results)))