Hi,
I have an app using AR that is threaded, and accesses multiple databases.
Sometimes I get MySQL server has gone away errors if a connection timed out due to a long period of inactivity.
So I would wish to verify the connections before using them.
Using verify_active_connections! is not possible, it is not thread safe. (In particular for Mysql it sends a stat request to every connection, which resets affected_rows, thus breaking optimistic locking which checks this.)
The problem is, there is not really an API for verify the current connections that a thread has.
The best I can do is this ugly line:
ActiveRecord::Base.connection_handler.connection_pools.each_value {| pool> pool.connection.verify!}
But this isn't ideal as this code will checkout a connection for *every* database in use, rather than just verify the ones that a particular thread is using (the ones that it already has checked out).
I suppose we could just check in all the thread's connections with clear_active_connections! so that they are verified on checkout then next time they are used. This seems clumsy though.
Could there be added a new method that does what I want? I could provide a patch if needed.
eg.
ActiveRecord::Base.verify_current_connections!
- Stephen