How to open and close a database connection with http_unix_daemon?

I want to open a connection to Postgresql with ODBC when I fire up a http server using something like

swipl server.pl --port=3030 --pidfile=http.pid

and close it when I do kill $(cat http.pid)

Would this work?

:- use_module(library(http/http_unix_daemon)).
:- initialization(run, main).

run :-
    setup_call_cleanup(
        odbc_connect(dbname, _, [alias(dbname)]),
        http_daemon,
        odbc_disconnect(dbname)
    ).

That probably works. Alternatively use at_halt/1. Iā€™d assume it is not too critical. The OS will close the network connection and the DB will disconnect. That may take some time. As the HTTP server is probably supposed to live long that is unlikely to cause any problems.

1 Like