# Long running prodigy database instance throws peewee.InterfaceError: connection already closed

**URL:** https://support.prodi.gy/t/long-running-prodigy-database-instance-throws-peewee-interfaceerror-connection-already-closed/5330
**Category:** Uncategorized
**Tags:** usage, database
**Created:** [February 18, 2022, 9:40am UTC](https://support.prodi.gy/t/long-running-prodigy-database-instance-throws-peewee-interfaceerror-connection-already-closed/5330 "2022-02-18T09:40:32Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![ctoto93](https://sea2.discourse-cdn.com/flex020/user_avatar/support.prodi.gy/ctoto93/32/2229_2.png) [@ctoto93](https://support.prodi.gy/u/ctoto93)
#### Post date: [February 18, 2022, 9:40am UTC](https://support.prodi.gy/t/long-running-prodigy-database-instance-throws-peewee-interfaceerror-connection-already-closed/5330/1 "2022-02-18T09:40:32Z")

</div>

Hi Prodigy Team,

We have a long-running flask application that opens a prodigy database connection. We encountered a problem, at some point, it throws an error `peewee.InterfaceError: connection already closed`.

I wonder what is the best practice to manage the database connection. The current setup is that we make a connection once (when the flask app is initialized) with the prodigy `connect` function and keep the database object reference for the entire application. Is it better to connect and close the database connection only when it is needed?

Note: we use Postgres as our database

---

<div class="post-metadata">

### Author: ![ines](https://sea2.discourse-cdn.com/flex020/user_avatar/support.prodi.gy/ines/32/3_2.png) [@ines](https://support.prodi.gy/u/ines)
#### Post date: [February 19, 2022, 11:46am UTC](https://support.prodi.gy/t/long-running-prodigy-database-instance-throws-peewee-interfaceerror-connection-already-closed/5330/2 "2022-02-19T11:46:54Z")

</div>

Hi! I think you might be running into a problem where `peewee` has already closed the database connection, which it manages under the hood. In theory, it should be fine to use the `connect` helper every time you need to access the Prodigy DB. If the connection is still open, `peewee` will use that, otherwise it will reconnect.

---

<div class="post-metadata">

### Author: ![ctoto93](https://sea2.discourse-cdn.com/flex020/user_avatar/support.prodi.gy/ctoto93/32/2229_2.png) [@ctoto93](https://support.prodi.gy/u/ctoto93)
#### Post date: [February 21, 2022, 10:00am UTC](https://support.prodi.gy/t/long-running-prodigy-database-instance-throws-peewee-interfaceerror-connection-already-closed/5330/3 "2022-02-21T10:00:25Z")

</div>

Hi Ines. Ahh okay, so this kind of error is expected if we keep the prodigy database object reference for a long period.

Okay, I'll try to use the `connect` helper function every time a query is needed.

Thank you for the answer 🙂
