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
12 ;;;; This file, part of CLSQL, is Copyright (c) 2002-2003 by Kevin M. Rosenberg
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 ;;;; *************************************************************************
19 (in-package #:clsql-sys)
21 (defvar *db-pool* (make-hash-table :test #'equal))
22 (defvar *db-pool-lock* (make-process-lock "DB Pool lock"))
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"))))
34 (defun acquire-from-conn-pool (pool)
35 "Try to find a working database connection in the pool or create a new
36 one if needed. This performs 1 query against the DB to ensure it's still
37 valid. When possible (postgres, mssql) that query will be a reset
38 command to put the connection back into its default state."
39 (or (with-process-lock ((conn-pool-lock pool) "Acquire from pool")
40 (when (plusp (length (free-connections pool)))
41 (let ((pconn (vector-pop (free-connections pool))))
42 ;; test if connection still valid.
43 ;; (e.g. db reboot -> invalid connection )
45 (case (database-underlying-type pconn)
47 ;; This resets the connection to "New" state
48 (database-execute-command "DISCARD ALL;" pconn))
50 (database-query "SHOW ERRORS LIMIT 1" pconn nil nil))
52 ;; rpc escape sequence since this can't be called as a normal sp.
53 ;;http://msdn.microsoft.com/en-us/library/aa198358%28SQL.80%29.aspx
54 (database-execute-command "{rpc sp_reset_connection}" pconn))
56 (database-query "SELECT 1;" pconn '(integer) nil)))
57 (sql-database-error (e)
58 ;; we could check for a specific error,
59 ;; but, it's safer just to disconnect the pooled conn for any error ?
60 (warn "Database connection ~S had an error when attempted to be acquired from the pool:
64 (ignore-errors (database-disconnect pconn))
66 (:no-error (&rest args)
67 (declare (ignore args))
69 (let ((conn (connect (connection-spec pool)
70 :database-type (pool-database-type pool)
73 (with-process-lock ((conn-pool-lock pool) "Acquire from pool")
74 (vector-push-extend conn (all-connections pool))
75 (setf (conn-pool conn) pool))
78 (defmethod release-to-conn-pool (conn)
79 (let ((pool (conn-pool conn)))
80 (with-process-lock ((conn-pool-lock pool) "Release to pool")
81 (vector-push-extend conn (free-connections pool)))))
83 (defun clear-conn-pool (pool)
84 (with-process-lock ((conn-pool-lock pool) "Clear pool")
85 (loop for conn across (all-connections pool)
86 do (setf (conn-pool conn) nil)
87 ;; disconnect may error if remote side closed connection
88 (ignore-errors (disconnect :database conn)))
89 (setf (fill-pointer (free-connections pool)) 0)
90 (setf (fill-pointer (all-connections pool)) 0))
93 (defun find-or-create-connection-pool (connection-spec database-type)
94 "Find connection pool in hash table, creates a new connection pool
96 (with-process-lock (*db-pool-lock* "Find-or-create connection")
97 (let* ((key (list connection-spec database-type))
98 (conn-pool (gethash key *db-pool*)))
100 (setq conn-pool (make-instance 'conn-pool
101 :connection-spec connection-spec
102 :pool-database-type database-type))
103 (setf (gethash key *db-pool*) conn-pool))
106 (defun acquire-from-pool (connection-spec database-type &optional pool)
107 (unless (typep pool 'conn-pool)
108 (setf pool (find-or-create-connection-pool connection-spec database-type)))
109 (acquire-from-conn-pool pool))
111 (defun release-to-pool (database)
112 (release-to-conn-pool database))
114 (defun disconnect-pooled (&optional clear)
115 "Disconnects all connections in the pool."
116 (with-process-lock (*db-pool-lock* "Disconnect pooled")
118 #'(lambda (key conn-pool)
119 (declare (ignore key))
120 (clear-conn-pool conn-pool))
122 (when clear (clrhash *db-pool*)))
125 ;(defun pool-start-sql-recording (pool &key (types :command))
126 ; "Start all stream in the pool recording actions of TYPES"
127 ; (dolist (con (pool-connections pool))
128 ; (start-sql-recording :type types
129 ; :database (connection-database con))))
131 ;(defun pool-stop-sql-recording (pool &key (types :command))
132 ; "Start all stream in the pool recording actions of TYPES"
133 ; (dolist (con (pool-connections pool))
134 ; (stop-sql-recording :type types
135 ; :database (connection-database con))))
137 ;(defmacro with-database-connection (pool &body body)
138 ; `(let ((connection (obtain-connection ,pool))
141 ; (with-database ((connection-database connection))
142 ; (setq results (multiple-value-list (progn ,@body))))
143 ; (release-connection connection))
144 ; (values-list results)))