Skip to content

Instantly share code, notes, and snippets.

@tamurashingo
Created August 21, 2017 21:27
Show Gist options
  • Save tamurashingo/2ec3f197140169e9005c532b347b59ac to your computer and use it in GitHub Desktop.
Save tamurashingo/2ec3f197140169e9005c532b347b59ac to your computer and use it in GitHub Desktop.
cl-batisサンプル
;; connection pool
(defvar *connection-pool* nil)
;; アプリケーションサーバが立ち上がるときに実行
(defun init-database (driver-name &rest params &key database-name &allow-other-keys)
"initialize database"
(setf *connection-pool* (apply #'dbi-cp:make-dbi-connection-pool driver-name params)))
;; アプリケーションサーバ終了時に実行
(defun shutdown-database ()
(dbi-cp:shutdown *connection-pool*))
;; こんな感じのマクロを定義
(defmacro transactional ((db) &body body)
"transactional macro like SpringFramework"
`(let ((,db (batis:create-sql-session *connection-pool*)))
(handler-case
(progn
,@body)
(error (e)
(progn
(batis:rollback ,db)
e))
(:no-error (c)
(batis:commit ,db)
c))))
;; サービス層でこんな感じで使いたい
(defun foo-service (&rest params)
"this is service"
(transactional (db)
(check-parameters params)
(register-user db params)
(fetch-user db params)))
;; こんな感じでやりたいけど db っていうパラメータが見えなくなるのでちょっと嫌な感じ
@Transactional
(defun foo-service (&rest params)
"this is service"
(check-parameters params)
(register-user db params)
(fetch-usre db params))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment