duanjiongzhen2523 2014-04-11 21:12
浏览 304
已采纳

什么决定了PDO :: ATTR_TIMEOUT如何与ODBC连接一起工作?

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.

  • 写回答

1条回答 默认 最新

  • dqxmf02844 2014-04-23 23:44
    关注

    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教