title: Reusing database connection for multiple requests
body: <p>If I don't need transactions, can I reuse the same database connection for multiple requests?</p>
  
  <p>Flask documentation <a href="http://flask.pocoo.org/docs/0.11/tutorial/dbcon/#step-3-database-connections" rel="nofollow noreferrer">says</a>:</p>
  
  <blockquote>
    <p>Because database connections encapsulate a transaction, we also need to make sure that only one request at the time uses the connection.</p>
  </blockquote>
  
  <p>Here's how I understand the meaning of the above sentence:</p>
  
  <blockquote>
    <p>Python DB-API connection can only handle one transaction at a time; to start a new transaction, one must first commit or roll back the previous one. So if each of our requests needs its own transaction, then of course each request needs its own database connection.</p>
  </blockquote>
  
  <p>Please let me know if I got it wrong. </p>
  
  <p>But let's say I set autocommit mode, and handle each request in a single SQL statement. Or, alternatively, let's say I only read - not write - to the database. In either case, <em>it seems</em> I can just reuse the same database connection for all my requests to save the overhead of multiple connections. But I'm not sure if there's any downside to this approach.</p>
  
  <p>Edit: I can see one issue with what I'm proposing: each request might be handled by a different process. Since connections should <a href="https://stackoverflow.com/questions/40482555/sharing-psycopg2-libpq-connections-across-processes">probably not be reused across processes</a>, let me clarify my question: I mean creating one connection per process, and using it for all requests that happen to be handled by this process.</p>
  
  <p>On the other hand, the whole point of (green or native) threads is usually to serve one request per thread, so my proposed approach implies sharing connection across threads. It seems <a href="http://initd.org/psycopg/docs/usage.html#thread-safety" rel="nofollow noreferrer">one connection can be used concurrently in multiple native threads</a>, but <a href="http://initd.org/psycopg/docs/advanced.html#support-for-coroutine-libraries" rel="nofollow noreferrer">not in multiple green threads</a>.</p>
  
  <p>So let's say for concreteness my environment is flask + gunicorn with multiple multi-threaded sync workers.</p>