in reply to Re: Preventing database handles from going stale
in thread Preventing database handles from going stale

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.

Replies are listed 'Best First'.
Re^3: Preventing database handles from going stale
by EvanK (Chaplain) on Feb 05, 2007 at 17:41 UTC
    Hmmm...I assume you're doing the queries through some kind of abstraction layer, rather than directly calling the DBI functions, which is causing the delay? If this is the case, you may need to modify said abstraction layer to do the "ping or reconnect" logic.

    Example code of one of these queries might help.

    __________
    The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.
    - Terry Pratchett

      Nope, I am using DBI directly, so the delay is very small. In practical terms, running the ping immediately before each query would probably be something over 99% effective, but it could never be 100% effective without implementing some kind of OS-level transactional capability to guarantee that Postgres doesn't get any processing time between the ping and the query. (And that's without even getting into cases where the db is running on a separate server with an independent CPU...)
        If an established connection to your DMBS is timing out in a matter of seconds, then my first guess is that it's misconfigured. I don't know what the default timeout is for Postgres is, but many DBs let you keep a connection open for hours, if not days. MySQL, for example, defaults to 28800 seconds (8 hours) IIRC.

        Other than that, dirving's solution of wrapping queries and reconnecting on failures is the only thing I can think of.

        __________
        The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.
        - Terry Pratchett