A
A
Alexander Rublev2016-03-27 21:02:25
MySQL
Alexander Rublev, 2016-03-27 21:02:25

Do I need to close the database connection?

Hello. I am writing a python program and using mysql connector.
I will have a lot of calls to the database in my program, but the program will also run for hours and no one will work in it.
Whether prompt it is necessary to close connection after each SQL request? For example like this:

conn = MySQLConnection("Данные для соединения")
        cursor = conn.cursor()
        cursor.execute("SELECT * FROM avi_db")
        row = cursor.fetchall()
        cursor.close()
        conn.close()

or is it better to close the connection after closing the program?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
DuD, 2016-03-27
@Meller008

If the application is a daemon, then it is better to reuse it. If each request raises a new process, then you can not close it, because. all connections will be cut off as soon as the request is processed.

V
Vov Vov, 2016-03-28
@balamut108

Make a Singleton on the connections and use it as you work. Reconnect every 10-15 minutes, because connections can still break, for example, the server itself will break or the grid, and indeed a bunch of all sorts of reasons.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question