X-Git-Url: http://git.kpe.io/?a=blobdiff_plain;f=sql%2Fpool.lisp;h=820789f03eba1696f6c1d628fedd40c8e0ef348a;hb=d86f73be9a261b9c071ab905aeff5d1ee30a3f31;hp=5f871532bb78d6cb8111ea63bbbc41e90eb5931e;hpb=6f6e687a1a642b26d42f82b6f4b9bb5e65343fa3;p=clsql.git diff --git a/sql/pool.lisp b/sql/pool.lisp index 5f87153..820789f 100644 --- a/sql/pool.lisp +++ b/sql/pool.lisp @@ -30,10 +30,10 @@ that should, on avg keep the free connections about this size.") (free-connections :accessor free-connections :initform nil) (all-connections :accessor all-connections :initform nil) (lock :accessor conn-pool-lock - :initform (make-process-lock "Connection pool")))) + :initform (make-process-lock "Connection pool")))) -(defun acquire-from-pool (connection-spec database-type &optional pool) +(defun acquire-from-pool (connection-spec database-type &optional pool encoding) "Try to find a working database connection in the pool or create a new one if needed. This performs 1 query against the DB to ensure it's still valid. When possible (postgres, mssql) that query will be a reset @@ -68,7 +68,8 @@ Disconnecting.~%" (let ((conn (connect (connection-spec pool) :database-type (pool-database-type pool) :if-exists :new - :make-default nil))) + :make-default nil + :encoding encoding))) (with-process-lock ((conn-pool-lock pool) "new conection") (push conn (all-connections pool)) (setf (conn-pool conn) pool)) @@ -85,8 +86,9 @@ chance to do cleanup." ;;not the list). Multiple threads getting to this test at the ;;same time might result in the free-connections getting ;;longer... meh. - ((>= (length (free-connections pool)) - *db-pool-max-free-connections*) + ((and *db-pool-max-free-connections* + (>= (length (free-connections pool)) + *db-pool-max-free-connections*)) (%pool-force-disconnect database) (with-process-lock ((conn-pool-lock pool) "Remove extra Conn") (setf (all-connections pool) @@ -120,6 +122,8 @@ chance to do cleanup." (database-execute-command "DISCARD ALL" database))))) (defun clear-conn-pool (pool) + "Be careful this function will disconnect connections without regard +to whether another thread is actively using them." (with-process-lock ((conn-pool-lock pool) "Clear pool") (mapc #'%pool-force-disconnect (all-connections pool)) (setf (all-connections pool) nil @@ -131,12 +135,12 @@ chance to do cleanup." if not found" (with-process-lock (*db-pool-lock* "Find-or-create connection") (let* ((key (list connection-spec database-type)) - (conn-pool (gethash key *db-pool*))) + (conn-pool (gethash key *db-pool*))) (unless conn-pool - (setq conn-pool (make-instance 'conn-pool - :connection-spec connection-spec - :pool-database-type database-type)) - (setf (gethash key *db-pool*) conn-pool)) + (setq conn-pool (make-instance 'conn-pool + :connection-spec connection-spec + :pool-database-type database-type)) + (setf (gethash key *db-pool*) conn-pool)) conn-pool))) (defun disconnect-pooled (&optional clear) @@ -145,8 +149,8 @@ the pool objects." (with-process-lock (*db-pool-lock* "Disconnect pooled") (maphash #'(lambda (key conn-pool) - (declare (ignore key)) - (clear-conn-pool conn-pool)) + (declare (ignore key)) + (clear-conn-pool conn-pool)) *db-pool*) (when clear (clrhash *db-pool*))) t)