title: what is the best practice for connecting to the database and manipulating data?
body: <p>some people said one connection per each request
  and some others said to create one connection and use it for each request.</p>
  <p>I think if we want to create one connection per request the number of requests that we can handle each time depends on our database.</p>
  <p>and if we want to create one connection for all requests we should always check our connection is alive.</p>
  <p>for example, in FastApi documentation, they said to use this code for access to the database.</p>
  <pre class="lang-py prettyprint-override"><code>def get_db():
      db = SessionLocal()
      try:
          yield db
      finally:
          db.close()
  </code></pre>
  <p>and my problem is when I send a lot of async requests to my application in some requests I got <code>sorry, too many clients already</code> error because of the number of connections that I can make to Postgres.</p>
  <p>I know I can change the pool_size like this in sqlarchmy</p>
  <pre class="lang-py prettyprint-override"><code>engine = create_engine(settings.SQLALCHEMY_DATABASE_URI, pool_size=200, max_overflow=0)
  </code></pre>
  <p>but is it a good way to fix my problem?</p>