Reusing a database connection for multiple queries

If I don't need transactions, can I reuse the same database connection for multiple queries?

The flask documentation says :

Since database connections encapsulate a transaction, we also need to make sure that only one query at a time uses the connection.

Here is how I understand the meaning of the above sentence:

A Python DB-API connection can only process one transaction at a time; To start a new transaction, you must first complete or cancel the previous one. Therefore, if each of our requests needs its own transaction, then, of course, each request needs its own connection to the database.

Please let me know if I am wrong.

But let me say that I set autocommit mode and process each query in a single SQL statement. Or, say, I only read - I don't write - to the database. In any case, it seems that I can simply reuse the same database connection for all my queries in order to save the overhead of multiple connections. But I'm not sure if there is a flaw in this approach.

Edit: I see one problem with what I propose: each request can be processed by another process. Since connections should probably not be reused in different processes , let me clarify my question: I mean creating one connection for each process and using it for all requests that this process can handle.

, ( ) , . , , .

, , - + gunicorn .

+2
1

@Craig Ringer , , .

- ( - , - ). , . psycopg2 (, , ) . , , , , .

+1

All Articles