Forráskód Böngészése

[db] keep sqlite connection

Innocenty Enikeew 8 éve
szülő
commit
f67bce7154
1 módosított fájl, 14 hozzáadás és 12 törlés
  1. 14 12
      db.lisp

+ 14 - 12
db.lisp

@@ -26,18 +26,20 @@
                    "sqlite connection lock") "Database connection lock")
 (defmacro with-db ((db) &body body)
   `(bordeaux-threads:with-recursive-lock-held (*db-lock*)
-     (if *current-db*
-         (let ((,db *current-db*))
-           (declare (ignorable ,db))
-           ,@body)
-         (sqlite:with-open-database (,db (db-path) :busy-timeout 30)
-           (sqlite:execute-non-query ,db "PRAGMA foreign_keys = ON")
-           (let ((*current-db* ,db))
-             (handler-case (progn ,@body)
-               (sqlite:sqlite-error (e)
-                 (log:error (sqlite:sqlite-error-code e)
-                            (sqlite:sqlite-error-message e))
-                 (error e))))))))
+     (unless *current-db*
+       (let ((path (db-path)))
+         (log:info "Connecting to ~A" path)
+         (setf *current-db* (sqlite:connect path))
+         (sqlite:execute-non-query *current-db* "PRAGMA foreign_keys = ON")))
+     (let ((,db *current-db*))
+       (declare (ignorable ,db))
+       (handler-case (progn ,@body)
+         (sqlite:sqlite-error (e)
+           (log:error (sqlite:sqlite-error-code e)
+                      (sqlite:sqlite-error-message e))
+           (ignore-errors (sqlite:disconnect *current-db*))
+           (setf *current-db* nil)
+           (error e))))))
 
 (defmacro db-transaction (&body body)
   (let ((db (gensym "DB-")))