Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do peewee models automatically close the connection?

I am using peewee to access a SQLite DB.

I have made a model.py like:

from peewee import *

db = SqliteDatabase('people.db')

class Person(Model):
    name = CharField()
    birthday = DateField()
    is_relative = BooleanField()

    class Meta:
        database = db

In another Python file (with import model) I then manipulate the DB with calls like Person.create() or Person.select(name=='Joe').delete_instance().

The Quickstart says at the end to call db.close() to close the connection. Does this apply to my case as well? Am I supposed to call something like model.db.close()?

like image 833
Superbest Avatar asked Jan 19 '17 08:01

Superbest


1 Answers

According to Charles Leifer, the maker of peewee it is the programmer's job to terminate connections. The documentation about Connection Pools tell, that all connections are thread-local, so as long as the Model is in use, the connection stays open and dies, if the thread containing the Transaction joins the Main-Thread.

Charles explicitly answers a question about the Connection Pool. The answer is a bit generalized, but I suppose this applies to all connections equally: About connection pool

Implicit answers on the topic:

Error 2006: MySQL server has gone away

Excerpt from the docs Quickstart Page:

Although it’s not necessary to open the connection explicitly, it is good practice since it will reveal any errors with your database connection immediately, as opposed to some arbitrary time later when the first query is executed. It is also good to close the connection when you are done – for instance, a web app might open a connection when it receives a request, and close the connection when it sends the response.

The final answer to your question is, based on these information: No.

like image 113
Semo Avatar answered Sep 20 '22 19:09

Semo