[racket] How to clean up a database connection in a program running in DrRacket?

From: Dmitry Pavlov (dpavlov at ipa.nw.ru)
Date: Fri Dec 26 16:03:15 EST 2014

Robby,

Thanks for the info! I just installed today's snapshot and the
connection now seems to disconnect itself when I restart the program.

Will executor for the connection still does not work, but since
the initial problem went away, I am not worried.

Best regards,

Dmitry

On 12/26/2014 09:10 PM, Robby Findler wrote:
> Which version of Racket are you using? I think Ryan pushed some
> custodian related improvements to the db library recently.
>
> Robby
>
> On Friday, December 26, 2014, Dmitry Pavlov <dpavlov at ipa.nw.ru
> <mailto:dpavlov at ipa.nw.ru>> wrote:
>
>     Hello,
>
>     Let us say I put this program into DrRacket's Definitions window:
>
>     ------------------------------__------------------------------__-----
>     #lang racket
>     (require db)
>
>     (define path "aaa.db")
>
>     (when (file-exists? path)
>        (delete-directory/files path))
>
>     (define conn (sqlite3-connect #:database path #:mode 'create))
>     ------------------------------__------------------------------__-----
>
>     and then press the "Run" button. After that, I play with conn
>     in the Interactions window. When I have played enough,
>     I press the "Run" button again. Here is what I get:
>
>     delete-file: cannot delete file
>        path: [...]\aaa.db
>        system error: Permission denied; errno=13
>
>     The reason is that I forgot to (disconnect conn) in the end.
>     (I get that only on Windows; Linux is OK with removing opened files).
>
>     How do I disconnect automatically? Putting the (disconnect conn)
>     into the Definitions window is not a solution, as I want to use
>     conn in the Interactions (also I want to be safe against the
>     "Stop" button pressed in the middle of the program before the
>     disconnect happens)
>
>     I tried to use a will executor:
>
>     ------------------------------__------------------------------__-----
>     (define an-executor (make-will-executor))
>     (void
>         (thread
>          (lambda ()
>            (let loop ()
>              (will-execute an-executor)
>              (loop)))))
>
>     (define (executor-proc v) (printf "I am now garbage\n") (disconnect v))
>     (will-register an-executor conn executor-proc)
>     ------------------------------__------------------------------__-----
>
>     Surprisingly, it did not work for the database connection (while it
>     does work for other kinds of objects), and I have no idea why. It
>     does not work even with I call the GC explicitly:
>
>     ------------------------------__------------------------------__-----
>     (set! conn #f)
>     (collect-garbage)
>     ------------------------------__------------------------------__-----
>
>     Is it possible to make the will executor work?
>     Generally speaking, what is the recommended way to do this kind
>     of thing?
>
>
>     Best regards,
>
>     Dmitry
>
>     ____________________
>       Racket Users list:
>     http://lists.racket-lang.org/__users
>     <http://lists.racket-lang.org/users>
>
>
>
> ____________________
>    Racket Users list:
>    http://lists.racket-lang.org/users
>

Posted on the users mailing list.