# How to open and close a database connection with http\_unix\_daemon?

**URL:** <https://swi-prolog.discourse.group/t/how-to-open-and-close-a-database-connection-with-http-unix-daemon/4669>\
**Category:** Help!\
**Created:** [November 21, 2021, 10:40am UTC](https://swi-prolog.discourse.group/t/how-to-open-and-close-a-database-connection-with-http-unix-daemon/4669 "2021-11-21T10:40:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![joeblog](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/joeblog/32/122_2.png) [@joeblog](https://swi-prolog.discourse.group/u/joeblog)\
**Post date:** [November 21, 2021, 10:40am UTC](https://swi-prolog.discourse.group/t/how-to-open-and-close-a-database-connection-with-http-unix-daemon/4669/1 "2021-11-21T10:40:40Z")

</div>

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

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

```

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

Would this work?

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

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

```

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [November 21, 2021, 4:21pm UTC](https://swi-prolog.discourse.group/t/how-to-open-and-close-a-database-connection-with-http-unix-daemon/4669/2 "2021-11-21T16:21:15Z")

</div>

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.
