help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: emacsql-mysql - best way to close connection?


From: John Mastro
Subject: Re: emacsql-mysql - best way to close connection?
Date: Sat, 1 Apr 2017 12:52:10 -0700

Guido Van Hoecke <address@hidden> wrote:
>> I create a mysql connection in a let* form:
>>
>> (let* ((host "mysql.host.com")
>>        (dummy (netrc-credentials host)) ;; needed to define remaining netrc 
>> stuff
>>        (info (netrc-machine (netrc-parse (expand-file-name "~/.netrc")) 
>> host))
>>        (id (cdr (assoc "login" info)))
>>        (pw (cdr (assoc "password" info)))
>>        (db (emacsql-mysql "database" :user id :password pw :host host)))
>>    ....)
>>
> I added (delete-process "emacsql-mysql") as last elisp form of the let*
> form. This does kill the process. Still I wonder if this is the way to go?

It looks like there's a function `emacsql-close' for that.

If your code that uses the connection may raise an error (which would
cause evaluation to never reach the call to `emacsql-close' or similar),
then you may want to wrap it in `unwind-protect':

    (let* (...
           (db (emacsql-mysql "database" :user id :password pw :host host)))
      (unwind-protect
          (progn ...)
        (emacsql-close db)))

Where you code that uses the connection would go inside the `progn'.

There's also a macro `emacsql-with-connection' that encapsulates this
pattern: create a connection, evaluate some forms, then ensure
`emacsql-close' is called even in the event of an error.

        John



reply via email to

[Prev in Thread] Current Thread [Next in Thread]