Skip to content Skip to sidebar Skip to footer

Reusing Database Connection For Multiple Requests

If I don't need transactions, can I reuse the same database connection for multiple requests? Flask documentation says: Because database connections encapsulate a transaction, we

Solution 1:

Based on @Craig Ringer comment on a different question, I think I know the answer.

The only possible advantage of connection sharing is performance (other factors - like transaction encapsulation and simplicity - favor a separate connection per request). And since a connection can't be shared across processes or green threads, it only has a chance with native threads. But psycopg2 (and presumably other drivers) doesn't allow concurrent access from the same connection. So unless each request spends very little time talking to the database, there is likely a performance hit, not benefit, from connection sharing.

Post a Comment for "Reusing Database Connection For Multiple Requests"