959481d3439f9512f9d375eaaf29266e0e3220b9
[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         (when (plusp (length (free-connections pool)))
37           (let ((pconn (vector-pop (free-connections pool))))
38             ;; test if connection still valid.
39             ;; Currently, on supported on MySQL
40             (cond
41               ((eq :mysql (database-type pconn))
42                (handler-case
43                    (database-query "SHOW ERRORS LIMIT 1" pconn nil nil)
44                  (error (e)
45                    ;; we could check for error type 2006 for "SERVER GONE AWAY",
46                    ;; but, it's safer just to disconnect the pooled conn for any error
47                    (warn "Database connection ~S had an error when attempted to be acquired from the pool:
48   ~S
49 Disconnecting.~%"
50                          pconn e)
51                    (disconnect :database pconn :error nil)
52                    nil)
53                  (:no-error (res fields)
54                    (declare (ignore res fields))
55                    pconn)))
56               (t
57                pconn)))))
58       (let ((conn (connect (connection-spec pool)
59                            :database-type (pool-database-type pool)
60                            :if-exists :new
61                            :make-default nil)))
62         (with-process-lock ((conn-pool-lock pool) "Acquire from pool")
63           (vector-push-extend conn (all-connections pool))
64           (setf (conn-pool conn) pool))
65         conn)))
66
67 (defun release-to-conn-pool (conn)
68   (let ((pool (conn-pool conn)))
69     (with-process-lock ((conn-pool-lock pool) "Release to pool")
70       (vector-push-extend conn (free-connections pool)))))
71
72 (defun clear-conn-pool (pool)
73   (with-process-lock ((conn-pool-lock pool) "Clear pool")
74     (loop for conn across (all-connections pool)
75           do (setf (conn-pool conn) nil)
76           ;; disconnect may error if remote side closed connection
77           (ignore-errors (disconnect :database conn)))
78     (setf (fill-pointer (free-connections pool)) 0)
79     (setf (fill-pointer (all-connections pool)) 0))
80   nil)
81
82 (defun find-or-create-connection-pool (connection-spec database-type)
83   "Find connection pool in hash table, creates a new connection pool
84 if not found"
85   (with-process-lock (*db-pool-lock* "Find-or-create connection")
86     (let* ((key (list connection-spec database-type))
87            (conn-pool (gethash key *db-pool*)))
88       (unless conn-pool
89         (setq conn-pool (make-instance 'conn-pool
90                                        :connection-spec connection-spec
91                                        :pool-database-type database-type))
92         (setf (gethash key *db-pool*) conn-pool))
93       conn-pool)))
94
95 (defun acquire-from-pool (connection-spec database-type &optional pool)
96   (unless (typep pool 'conn-pool)
97     (setf pool (find-or-create-connection-pool connection-spec database-type)))
98   (acquire-from-conn-pool pool))
99
100 (defun release-to-pool (database)
101   (release-to-conn-pool database))
102
103 (defun disconnect-pooled (&optional clear)
104   "Disconnects all connections in the pool."
105   (with-process-lock (*db-pool-lock* "Disconnect pooled")
106     (maphash
107      #'(lambda (key conn-pool)
108          (declare (ignore key))
109          (clear-conn-pool conn-pool))
110      *db-pool*)
111     (when clear (clrhash *db-pool*)))
112   t)
113
114 ;(defun pool-start-sql-recording (pool &key (types :command))
115 ;  "Start all stream in the pool recording actions of TYPES"
116 ;  (dolist (con (pool-connections pool))
117 ;    (start-sql-recording :type types
118 ;                        :database (connection-database con))))
119
120 ;(defun pool-stop-sql-recording (pool &key (types :command))
121 ;  "Start all stream in the pool recording actions of TYPES"
122 ;  (dolist (con (pool-connections pool))
123 ;    (stop-sql-recording :type types
124 ;                         :database (connection-database con))))
125
126 ;(defmacro with-database-connection (pool &body body)
127 ;  `(let ((connection (obtain-connection ,pool))
128 ;         (results nil))
129 ;    (unwind-protect
130 ;         (with-database ((connection-database connection))
131 ;           (setq results (multiple-value-list (progn ,@body))))
132 ;      (release-connection connection))
133 ;    (values-list results)))