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
10 ;;;; This file, part of CLSQL, is Copyright (c) 2002-2010 by Kevin M. Rosenberg
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 ;;;; *************************************************************************
17 (in-package #:clsql-sys)
19 (defparameter *db-pool-max-free-connections* 4
20 "Threshold of free-connections in the pool before we disconnect a
21 database rather than returning it to the pool. This is really a heuristic
22 that should, on avg keep the free connections about this size.")
24 (defvar *db-pool* (make-hash-table :test #'equal))
25 (defvar *db-pool-lock* (make-process-lock "DB Pool lock"))
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"))))
36 (defun acquire-from-pool (connection-spec database-type &optional pool)
37 "Try to find a working database connection in the pool or create a new
38 one if needed. This performs 1 query against the DB to ensure it's still
39 valid. When possible (postgres, mssql) that query will be a reset
40 command to put the connection back into its default state."
41 (unless (typep pool 'conn-pool)
42 (setf pool (find-or-create-connection-pool connection-spec database-type)))
44 (loop for pconn = (with-process-lock ((conn-pool-lock pool) "Acquire")
45 (pop (free-connections pool)))
48 ;; test if connection still valid.
49 ;; (e.g. db reboot -> invalid connection )
51 (progn (database-acquire-from-conn-pool pconn)
53 (sql-database-error (e)
54 ;; we could check for a specific error,
55 ;; but, it's safer just to disconnect the pooled conn for any error ?
56 (warn "Database connection ~S had an error while acquiring from the pool:
60 ;;run database disconnect to give chance for cleanup
61 ;;there, then remove it from the lists of connected
63 (%pool-force-disconnect pconn)
64 (with-process-lock ((conn-pool-lock pool) "remove dead conn")
65 (setf (all-connections pool)
66 (delete pconn (all-connections pool))))
68 (let ((conn (connect (connection-spec pool)
69 :database-type (pool-database-type pool)
72 (with-process-lock ((conn-pool-lock pool) "new conection")
73 (push conn (all-connections pool))
74 (setf (conn-pool conn) pool))
77 (defun release-to-pool (database)
78 "Release a database connection to the pool. The backend will have a
79 chance to do cleanup."
80 (let ((pool (conn-pool database)))
82 ;;We read the list of free-connections outside the lock. This
83 ;;should be fine as long as that list is never dealt with
84 ;;destructively (push and pop destructively modify the place,
85 ;;not the list). Multiple threads getting to this test at the
86 ;;same time might result in the free-connections getting
88 ((and *db-pool-max-free-connections*
89 (>= (length (free-connections pool))
90 *db-pool-max-free-connections*))
91 (%pool-force-disconnect database)
92 (with-process-lock ((conn-pool-lock pool) "Remove extra Conn")
93 (setf (all-connections pool)
94 (delete database (all-connections pool)))))
97 (database-release-to-conn-pool database)
98 (with-process-lock ((conn-pool-lock pool) "Release to pool")
99 (push database (free-connections pool)))))))
101 (defmethod database-acquire-from-conn-pool (database)
102 (case (database-underlying-type database)
104 (database-execute-command "RESET ALL" database))
106 (database-query "SHOW ERRORS LIMIT 1" database nil nil))
108 ;; rpc escape sequence since this can't be called as a normal sp.
109 ;;http://msdn.microsoft.com/en-us/library/aa198358%28SQL.80%29.aspx
110 (database-execute-command "{rpc sp_reset_connection}" database))
112 (database-query "SELECT 1;" database '(integer) nil))))
114 (defmethod database-release-to-conn-pool (database)
115 (case (database-underlying-type database)
118 ;;http://www.postgresql.org/docs/current/static/sql-discard.html
119 ;;this was introduced relatively recently, wrap in ignore-errors
120 ;;so that it doesn't choke older versions.
121 (database-execute-command "DISCARD ALL" database)))))
123 (defun clear-conn-pool (pool)
124 (with-process-lock ((conn-pool-lock pool) "Clear pool")
125 (mapc #'%pool-force-disconnect (all-connections pool))
126 (setf (all-connections pool) nil
127 (free-connections pool) nil))
130 (defun find-or-create-connection-pool (connection-spec database-type)
131 "Find connection pool in hash table, creates a new connection pool
133 (with-process-lock (*db-pool-lock* "Find-or-create connection")
134 (let* ((key (list connection-spec database-type))
135 (conn-pool (gethash key *db-pool*)))
137 (setq conn-pool (make-instance 'conn-pool
138 :connection-spec connection-spec
139 :pool-database-type database-type))
140 (setf (gethash key *db-pool*) conn-pool))
143 (defun disconnect-pooled (&optional clear)
144 "Disconnects all connections in the pool. When clear, also deletes
146 (with-process-lock (*db-pool-lock* "Disconnect pooled")
148 #'(lambda (key conn-pool)
149 (declare (ignore key))
150 (clear-conn-pool conn-pool))
152 (when clear (clrhash *db-pool*)))
155 (defun %pool-force-disconnect (database)
156 "Force disconnection of a connection from the pool."
157 ;;so it isn't just returned to pool
158 (setf (conn-pool database) nil)
159 ;; disconnect may error if remote side closed connection
160 (ignore-errors (disconnect :database database)))
162 ;(defun pool-start-sql-recording (pool &key (types :command))
163 ; "Start all stream in the pool recording actions of TYPES"
164 ; (dolist (con (pool-connections pool))
165 ; (start-sql-recording :type types
166 ; :database (connection-database con))))
168 ;(defun pool-stop-sql-recording (pool &key (types :command))
169 ; "Start all stream in the pool recording actions of TYPES"
170 ; (dolist (con (pool-connections pool))
171 ; (stop-sql-recording :type types
172 ; :database (connection-database con))))
174 ;(defmacro with-database-connection (pool &body body)
175 ; `(let ((connection (obtain-connection ,pool))
178 ; (with-database ((connection-database connection))
179 ; (setq results (multiple-value-list (progn ,@body))))
180 ; (release-connection connection))
181 ; (values-list results)))