<p dir="auto"><strong>Migrated issue, originally created by Daniel Birnstiel (<a href="https://github.com/Birne94">@Birne94</a>)</strong></p>
<p dir="auto">I am currently receiving the exception mentioned in the title on a production deployment of a flask application using SQLAlchemy.</p>
<p dir="auto">This issue has happened before (closed in <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="384615968" data-permission-text="Title is private" data-url="https://github.com/sqlalchemy/sqlalchemy/issues/1246" data-hovercard-type="issue" data-hovercard-url="/sqlalchemy/sqlalchemy/issues/1246/hovercard" href="https://github.com/sqlalchemy/sqlalchemy/issues/1246">#1246</a> and <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="384624476" data-permission-text="Title is private" data-url="https://github.com/sqlalchemy/sqlalchemy/issues/2317" data-hovercard-type="issue" data-hovercard-url="/sqlalchemy/sqlalchemy/issues/2317/hovercard" href="https://github.com/sqlalchemy/sqlalchemy/issues/2317">#2317</a>), but appears to have resurfaced again.</p>
<p dir="auto">Some info about my environment:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="% py --version
Python 3.4.3
% pip freeze | grep SQLAlchemy
SQLAlchemy==1.1.4"><pre class="notranslate"><code class="notranslate">% py --version
Python 3.4.3
% pip freeze | grep SQLAlchemy
SQLAlchemy==1.1.4
</code></pre></div>
<p dir="auto">Database is an RDS instance on AWS using psycopg2==2.6.2 as a driver.</p>
<p dir="auto">Full exception log: <a href="https://gist.github.com/Birne94/e6e7ba91d44a680ba09419f7aa9f5705">https://gist.github.com/Birne94/e6e7ba91d44a680ba09419f7aa9f5705</a> (also attached to this report)</p>
<p dir="auto">The exception log shows two different exceptions happening:</p>
<ul dir="auto">
<li>
<p dir="auto">At first the server closes a connection, most likely due to a restart on the side of the database server. The query fails, page errors. I have a custom exception handler which will rollback the failed database session. Since this was a simple SELECT, I guess this is not needed (right?).</p>
</li>
<li>
<p dir="auto">Further access to the database fails with the exception mentioned in the title. As mentioned above, the failed transation was rolled back so there shouldn't be any issue with this one still existing. Again, the query was a simple select which should not start any transaction.</p>
</li>
</ul>
<p dir="auto">After restarting the server (uwsgi), everything went back to normal. Unfortunately, it was a priority to get the server up and running again, so I was unable to further investigate the issue by attaching a debugger.</p>
<p dir="auto">I would guess that SQLAlchemy should be able to handle closed connections and be able to reconnect automatically.</p>
<p dir="auto">If this is not the case, is there anything I can do to detect operations on an invalid connection and perform a manual reconnect?</p>
<p dir="auto">Thanks for reading.</p>
<p dir="auto"><strong>Migrated issue, originally created by Michael Bayer (<a href="https://github.com/zzzeek">@zzzeek</a>)</strong></p>
<p dir="auto">When a pool can't connect to the DB, Python 3 does this:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="#!

Traceback (most recent call last):
  File &quot;/usr/local/lib64/python3.5/site-packages/sqlalchemy/pool.py&quot;, line 1122, in _do_get
    return self._pool.get(wait, self._timeout)
  File &quot;/usr/local/lib64/python3.5/site-packages/sqlalchemy/util/queue.py&quot;, line 145, in get
    raise Empty
sqlalchemy.util.queue.Empty
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File &quot;/usr/local/lib64/python3.5/site-packages/sqlalchemy/engine/base.py&quot;, line 2147, in _wrap_pool_connect
    return fn()
  File &quot;/usr/local/lib64/python3.5/site-packages/sqlalchemy/pool.py&quot;, line 328, in unique_connection
    return _ConnectionFairy._checkout(self)
  File &quot;/usr/local/lib64/python3.5/site-packages/sqlalchemy/pool.py&quot;, line 766, in _checkout
    fairy = _ConnectionRecord.checkout(pool)
  File &quot;/usr/local/lib64/python3.5/site-packages/sqlalchemy/pool.py&quot;, line 516, in checkout
    rec = pool._do_get()
  File &quot;/usr/local/lib64/python3.5/site-packages/sqlalchemy/pool.py&quot;, line 1138, in _do_get
    self._dec_overflow()
  File &quot;/usr/local/lib64/python3.5/site-packages/sqlalchemy/util/langhelpers.py&quot;, line 66, in __exit__
    compat.reraise(exc_type, exc_value, exc_tb)
  File &quot;/usr/local/lib64/python3.5/site-packages/sqlalchemy/util/compat.py&quot;, line 187, in reraise
    raise value
  File &quot;/usr/local/lib64/python3.5/site-packages/sqlalchemy/pool.py&quot;, line 1135, in _do_get
    return self._create_connection()
  File &quot;/usr/local/lib64/python3.5/site-packages/sqlalchemy/pool.py&quot;, line 333, in _create_connection
    return _ConnectionRecord(self)
  File &quot;/usr/local/lib64/python3.5/site-packages/sqlalchemy/pool.py&quot;, line 461, in __init__
    self.__connect(first_connect_check=True)
  File &quot;/usr/local/lib64/python3.5/site-packages/sqlalchemy/pool.py&quot;, line 651, in __connect
    connection = pool._invoke_creator(self)
  File &quot;/usr/local/lib64/python3.5/site-packages/sqlalchemy/engine/strategies.py&quot;, line 105, in connect
    return dialect.connect(*cargs, **cparams)
  File &quot;/usr/local/lib64/python3.5/site-packages/sqlalchemy/engine/default.py&quot;, line 393, in connect
    return self.dbapi.connect(*cargs, **cparams)
  File &quot;/usr/local/lib64/python3.5/site-packages/MySQLdb/__init__.py&quot;, line 81, in Connect
    return Connection(*args, **kwargs)
  File &quot;/usr/local/lib64/python3.5/site-packages/MySQLdb/connections.py&quot;, line 191, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (2003, &quot;Can't connect to MySQL server on 'dbhost_obviously' (4)&quot;)"><pre class="notranslate"><code class="notranslate">#!

Traceback (most recent call last):
  File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/pool.py", line 1122, in _do_get
    return self._pool.get(wait, self._timeout)
  File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/util/queue.py", line 145, in get
    raise Empty
sqlalchemy.util.queue.Empty
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/engine/base.py", line 2147, in _wrap_pool_connect
    return fn()
  File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/pool.py", line 328, in unique_connection
    return _ConnectionFairy._checkout(self)
  File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/pool.py", line 766, in _checkout
    fairy = _ConnectionRecord.checkout(pool)
  File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/pool.py", line 516, in checkout
    rec = pool._do_get()
  File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/pool.py", line 1138, in _do_get
    self._dec_overflow()
  File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/util/langhelpers.py", line 66, in __exit__
    compat.reraise(exc_type, exc_value, exc_tb)
  File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/util/compat.py", line 187, in reraise
    raise value
  File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/pool.py", line 1135, in _do_get
    return self._create_connection()
  File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/pool.py", line 333, in _create_connection
    return _ConnectionRecord(self)
  File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/pool.py", line 461, in __init__
    self.__connect(first_connect_check=True)
  File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/pool.py", line 651, in __connect
    connection = pool._invoke_creator(self)
  File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/engine/strategies.py", line 105, in connect
    return dialect.connect(*cargs, **cparams)
  File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/engine/default.py", line 393, in connect
    return self.dbapi.connect(*cargs, **cparams)
  File "/usr/local/lib64/python3.5/site-packages/MySQLdb/__init__.py", line 81, in Connect
    return Connection(*args, **kwargs)
  File "/usr/local/lib64/python3.5/site-packages/MySQLdb/connections.py", line 191, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on 'dbhost_obviously' (4)")
</code></pre></div>
<p dir="auto">this confuses everyone.  move the handling outside of Empty.</p>