Skip to content

Commit

Permalink
Disconnect connections which is opened from dead threads.
Browse files Browse the repository at this point in the history
  • Loading branch information
fukamachi committed Dec 18, 2016
1 parent 20658c6 commit 48a50ef
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/dbi.lisp
Expand Up @@ -20,6 +20,9 @@
:rollback
:ping
:row-count)
(:import-from :bordeaux-threads
:current-thread
:thread-alive-p)
(:export :list-all-drivers
:find-driver
:connection-driver-type
Expand Down Expand Up @@ -67,8 +70,6 @@

(apply #'make-connection (make-instance driver) params)))

(defparameter *connection-pool* nil)

(defun make-connection-pool ()
(make-hash-table :test 'equal))

Expand All @@ -94,14 +95,27 @@
(conn (gethash connect-args pool)))
(cond
((null conn)
(cleanup-connection-pool)
(setf (gethash connect-args pool)
(apply #'connect connect-args)))
((not (ping conn))
(disconnect conn)
(remhash connect-args pool)
(apply #'connect-cached connect-args))
(cleanup-connection-pool)
(setf (gethash connect-args pool)
(apply #'connect connect-args)))
(t conn))))

(defun cleanup-connection-pool ()
(maphash (lambda (thread pool)
(unless (bt:thread-alive-p thread)
(maphash (lambda (args conn)
(declare (ignore args))
(disconnect conn))
pool)
(remhash thread *threads-connection-pool*)))
*threads-connection-pool*))

(defun load-driver (driver-name)
(let ((driver-system (intern (format nil "DBD-~A" driver-name) :keyword)))
#+quicklisp (ql:quickload driver-system :verbose nil :silent t)
Expand Down

0 comments on commit 48a50ef

Please sign in to comment.