>
> Using SQLAlchemy 0.4.4
>
> I am having a problem with SQLAlchemy where after the application that
> uses SQLAlchemy has been sitting overnight and a user makes the first
> queries of the day thru the app that uses SA, SQLAlchemy throws an
> error saying 'MySQL server has gone away', which I understand the
> reason to be that my mysql server has cut the idle connections, but I
> have pool_recycle = 3600 set and I thought that was the solution to
> keeping connections alive?
>
> After that error message I get two more exceptions when the user tries
> the request again of "Can't reconnect until invalid transaction is
> rolled back", which I don't understand at all because the application
> only queries the DB and never deals with transactions. After those
> three attempts, the forth request will go through just fine. :-\ Any
> pointers on what I should look for or do?
this indicates your application has checked out a Connection from the
pool and is keeping it open, in a transaction. the connection then
times out, and upon reaccess the next day would like to reconnect
itself on next access; for a current Connection, this can reconnect
its internal DBAPI connection transparently and you get to use the
same Connection object. But Connection won't allow you to do this if
its in a transaction, i.e. begin(); when the disconnect exception is
thrown, it expects that to be within a try/except block which will do
a rollback on error.
The reason you're getting the disconnect exception in the first place
is because the pool_recycle feature only works upon checkout from the
pool. So the solution is the same, ensure all connections are
returned to the pool after operations are complete.
using transactional=False is one solution, but a better one is to
simply rollback(), commit(), or close() the Session when operations
are complete - transactional mode (which is called "autocommit=False"
in 0.5) has the advantage that a series of select operations will all
share the same isolated transactional context..this can be more or
less important depending on the isolation mode in effect and the kind
of application.
DBAPI has no implicit "autocommit" mode so there is always a
transaction implicitly in progress when queries are made.