使用 Zend_Db_Adapter_Pdo_Mssql 时可以设置查询超时吗?

问题描述:

我们有一个 PHP 应用程序(托管在 Linux 上),它使用 Zend Framework 组件来查询 Microsoft SQL Server 2008 数据库.PHP 应用程序托管在具有可靠互联网连接的数据中心,但 SQL Server 数据库位于 VPN 连接的远端,该连接经常断开.

We have a PHP application (hosted on Linux) which uses Zend Framework components to query a Microsoft SQL Server 2008 database. The PHP application is hosted in a datacenter with reliable internet connection, but the SQL Server database is at the far end of a VPN connection that drops out regularly.

我们遇到的问题是在对 SQL 服务器进行查询时偶尔会发生 VPN 掉线.发生这种情况时,我们的应用程序最多可能等待 2 小时,然后才会最终引发以下异常:

The issue we have is that VPN drop outs occasionally occur while queries against the SQL server are in progress. When this occurs our application can wait up to 2 hours before finally raising the following exception:

SQLSTATE[HY000]: General error: 20004 Read from the server failed [20004] (severity 9) [(null)]

我想做的是将整体查询超时和/或读取超时设置为大约 2-3 分钟,以便应用程序更早地获得异常并从中恢复,而不会阻塞 2 小时.

What I'd like to do is set an overall query timeout and/or read timeout of around 2-3 minutes so that the application gets an exception much earlier and con recover from it without blocking for 2 hours.

我们使用 pdo_dblib 扩展连接到 SQL Server,我已经浏览了 php.net 文档,但我找不到任何用于连接 php.ini 的超时选项.

We're using the pdo_dblib extension to connect to SQL Server, and I've been through the php.net docs and I can't find any timeout options either for the connection of php.ini.

尝试使用 PDO::ATTR_TIMEOUT 属性.我不确定 pdo_dblib 的默认值是什么,它可能因驱动程序而异.

Try using the PDO::ATTR_TIMEOUT attribute. I'm not sure what the default value is for pdo_dblib, it may differ between the drivers.