Question

The documentation for ATTR_TIMEOUT says:

Specifies the timeout duration in seconds. Not all drivers support this option, and it's meaning may differ from driver to driver. For example, sqlite will wait for up to this time value before giving up on obtaining an writable lock, but other drivers may interpret this as a connect or a read timeout interval.

I am using an ODBC connection via unixODBC to a closed-source database called Vertica, and I am not seeing the proper connection timeout behavior when I connect via:

$this->conn = new PDO($dsn, $user, $password,
                      array(PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
                            PDO::ATTR_TIMEOUT => 2));   // timeout in 2 seconds

Is that behavior provided by the PDO PHP class, or unixODBC, or the Vertica ODBC driver itself? Trying to figure out where to start debugging.

Was it helpful?

Solution

I ended up solving this simply by attempting to open a TCP connection to vertica with a timeout (on the correct port), and immediately closing on a successful connection. While this does not guarantee that I am actually talking to a healthy Vertica database, it is sufficient for my own use case.

Something like this seems to be working OK:

protected function isAlive() {
    $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    socket_set_nonblock($socket);
    for ($i=0; $i<5; $i++) {
        if(!@socket_connect($socket, $this->host, $this->port)) {
            usleep(100000);                 // sleep for 0.1 seconds
        } else {
            return true;
        }
    }
    return false;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top