D
D
Dmitry Dudnik2020-01-07 19:33:36
Database
Dmitry Dudnik, 2020-01-07 19:33:36

What is the best way to connect and disconnect from the database?

I am writing a telegram bot in python. The data is saved to the database.
And the question is, how is it better to open a connection for the entire time the bot is running, or open a connection before executing a request or a group of requests and then close the connection until the next requests?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
feogil, 2020-03-27
@feogil

Creating a connection is usually quite a costly operation.
Much depends on the database used and the frequency of queries (if a query to the database is required once an hour, for example, does it make sense to keep the connection open?), but in practice the first option is usually used, several (rarely one) connections to the database are created (connection pool), which are used during the operation of the application, occasionally updated.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question