Database connections hanging

Hello friends,

I’m trying to set-up a remote database for my Rails application (mysql-server running on separate VM). My host (Azure) has an idle-timeout setting of 4 minutes on all TCP connections. Because of that, if a Rails process is idle for more than 4 minutes, the connection hangs. The process holding the connection (Unicorn, Sidekiq) also hangs.

The backtrace shows that the process is stuck at ping -

activerecord-5.0.0/lib/active_record/connection_adapters/mysql2_adapter.rb:94:in `ping’

activerecord-5.0.0/lib/active_record/connection_adapters/mysql2_adapter.rb:94:in `active?’

activerecord-5.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:400:in `verify!’

activerecord-5.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:770:in `block in checkout_and_verify’

For now I have implemented a sort of heat-beat that keeps the Unicorns/Sidekiq connections alive, but I feel its fragile. I wonder if there is a way here to tell from the Rails side that the connection has gone? Or may be apply some kind of timeout on ping itself ?

Please share any ideas or suggestions this.

Thanks in advance,