X-Git-Url: http://git.kpe.io/?a=blobdiff_plain;f=sql%2Fpool.cl;h=bf790a46c58b6dae9034ef5204c52f44ea572a6f;hb=1785177d3364ad0ad8917193b5b00310ef88105e;hp=14efa2326ddbc6131e2faeb5a7967f3eeeab0e85;hpb=c86ceae4227949bc6a43469d265fbac3d7355d4e;p=clsql.git diff --git a/sql/pool.cl b/sql/pool.cl index 14efa23..bf790a4 100644 --- a/sql/pool.cl +++ b/sql/pool.cl @@ -4,10 +4,10 @@ ;;;; ;;;; Name: pool.cl ;;;; Purpose: Support function for connection pool -;;;; Programmers: Kevin M. Rosenberg +;;;; Programmers: Kevin M. Rosenberg, Marc Battyani ;;;; Date Started: Apr 2002 ;;;; -;;;; $Id: pool.cl,v 1.2 2002/04/28 00:50:17 kevin Exp $ +;;;; $Id: pool.cl,v 1.3 2002/05/01 20:22:16 marc.battyani Exp $ ;;;; ;;;; This file, part of CLSQL, is Copyright (c) 2002 by Kevin M. Rosenberg ;;;; @@ -21,39 +21,70 @@ (defvar *db-pool* (make-hash-table :test #'equal)) -(defun make-conn-vector () - "Creates an empty connection vector" - (make-array 5 :fill-pointer 0 :adjustable t)) +(defclass conn-pool () + ((connection-spec :accessor connection-spec :initarg :connection-spec) + (database-type :accessor database-type :initarg :database-type) + (free-connections :accessor free-connections + :initform (make-array 5 :fill-pointer 0 :adjustable t)) + (all-connections :accessor all-connections + :initform (make-array 5 :fill-pointer 0 :adjustable t)))) -(defun find-or-create-conn-vector (connection-spec database-type) +(defun acquire-from-conn-pool (pool) + (if (zerop (length (free-connections pool))) + (let ((conn (connect (connection-spec pool) + :database-type (database-type pool) :if-exists :new))) + (vector-push-extend conn (all-connections pool)) + (setf (conn-pool conn) pool) + conn) + (vector-pop (free-connections pool)))) + +(defun release-to-conn-pool (conn) + (vector-push-extend conn (free-connections (conn-pool conn)))) + +(defun clear-conn-pool (pool) + (loop for conn across (all-connections pool) + do (disconnect :database conn)) + (setf (fill-pointer (free-connections pool)) 0) + (setf (fill-pointer (all-connections pool)) 0)) + +(defun find-or-create-conn-pool (connection-spec database-type) "Find connection vector in hash table, creates a new conn-vector if not found" (let* ((key (list connection-spec database-type)) - (conn-vector (gethash key *db-pool*))) - (unless conn-vector - (setq conn-vector (make-conn-vector)) - (setf (gethash key *db-pool*) conn-vector)) - conn-vector)) - -(defun acquire-from-pool (connection-spec database-type) - (let ((conn-vector (find-or-create-conn-vector connection-spec database-type))) - (when (zerop (length conn-vector)) - (vector-push-extend - (connect connection-spec :database-type database-type :if-exists :new) - conn-vector)) - (vector-pop conn-vector))) + (conn-pool (gethash key *db-pool*))) + (unless conn-pool + (setq conn-pool (make-instance 'conn-pool + :connection-spec connection-spec + :database-type database-type)) + (setf (gethash key *db-pool*) conn-pool)) + conn-pool)) + +(defun acquire-from-pool (connection-spec database-type &optional pool) + (unless pool (setf pool (find-or-create-conn-pool connection-spec database-type))) + (acquire-from-conn-pool pool)) (defun release-to-pool (database) - (let ((conn-vector (find-or-create-conn-vector (connection-spec database) - (database-type database)))) - (vector-push-extend database conn-vector))) + (release-to-conn-pool database)) -(defun disconnect-pooled () +(defun disconnect-pooled (&optional clear) "Disconnects all connections in the pool" (maphash - #'(lambda (key conn-vector) + #'(lambda (key conn-pool) (declare (ignore key)) - (dotimes (i (length conn-vector)) - (disconnect (aref conn-vector i))) - (setf (fill-pointer conn-vector) 0)) + (clear-conn-pool conn-pool)) *db-pool*) + (when clear (clrhash *db-pool*)) t) + +;;; with-db-from-pool is the macro you should use if you want to use pooled connections. +;;; You can use it with a connection spec and database type or directly with a conn-pool. +;;; When you give a conn-pool the connection spec and database type are ignored + +(defmacro with-db-from-pool ((db-var connection-spec database-type &optional conn-pool) &body body) + "Evaluate the body in an environment, where `db-var' is bound to a +database connection acquired from the connection pool +The connection is automatically released to the connection pool on exit from the body. +If a pool is given then the connection-spec database-type are ignored." + `(let ((,db-var (acquire-from-pool ,connection-spec ,database-type ,conn-pool))) + (unwind-protect + (let ((,db-var ,db-var)) ,@body) + (release-to-pool ,db-var))))