Aside from using connect_cached, that's basically the same as what my current ping || reinit_db is doing and has the same problem: $dbh can go stale after the ping but before the query. Calling it before each query reduces the odds of that happening by reducing the time between the two events, but can't prevent it entirely.