Issue 1:
Migrated issue, originally created by Daniel Birnstiel (@Birne94)

I am currently receiving the exception mentioned in the title on a production deployment of a flask application using SQLAlchemy.

This issue has happened before (closed in #1246 and #2317), but appears to have resurfaced again.

Some info about my environment:

% py --version
Python 3.4.3
% pip freeze | grep SQLAlchemy
SQLAlchemy==1.1.4

Database is an RDS instance on AWS using psycopg2==2.6.2 as a driver.

Full exception log: https://gist.github.com/Birne94/e6e7ba91d44a680ba09419f7aa9f5705 (also attached to this report)

The exception log shows two different exceptions happening:

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?).

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.

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.

I would guess that SQLAlchemy should be able to handle closed connections and be able to reconnect automatically.

If this is not the case, is there anything I can do to detect operations on an invalid connection and perform a manual reconnect?

Thanks for reading.

Issue 2:
Migrated issue, originally created by Michael Bayer (@zzzeek)

When a pool can't connect to the DB, Python 3 does this:

#!

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)")

this confuses everyone. move the handling outside of Empty.