douren1928 2014-08-12 14:34
浏览 88
已采纳

FreeTDS:正确关闭PHP PDO连接,无法正常工作

I have an old CodeIgniter instance that I'm trying to use with PHP PDO. This is based on the "cickes" modifications (see links below). It works well under small request loads, but the connections seem to hang for a long while under heavy request loads. Eventually I run out of available connections and the DB returns and error: SQLSTATE[] (null) (severity 0).

https://github.com/cickes/PDOinCodeigniter2

I'm using a MS SQL Server 2008 with PHP PDO. The PHP driver for MSSQL is the FreeTDS driver which can be seen with phpinfo() output. FreeTDS PHPINFO shown below:

mssql
MSSQL Support   enabled
Active Persistent Links 1
Active Links    1
Library version FreeTDS
Directive   Local Value Master Value
mssql.allow_persistent  On  On
mssql.batchsize 0   0
mssql.charset   no value    no value
mssql.compatability_mode    Off Off
mssql.connect_timeout   5   5
mssql.datetimeconvert   On  On
mssql.max_links Unlimited   Unlimited
mssql.max_persistent    Unlimited   Unlimited
mssql.max_procs Unlimited   Unlimited
mssql.min_error_severity    10  10
mssql.min_message_severity  10  10
mssql.secure_connection Off Off
mssql.textlimit Server default  Server default
mssql.textsize  Server default  Server default
mssql.timeout   60  60

I've set the DB handler to null as well as unset() the DB handler. Both changes do not help. Eventually I run out of connections.

I thought that I could try to pass in an attribute options during connection, as suggested on the PHP Web site. Unfortunately, the FreeTDS driver does not support attributes. The first $DB defined is working, but I believe it uses persistent connections.

$DB = new PDO($params['dbdriver'].':host='.$params['hostname'].'; dbname='.$params['database'], $params['username'], $params['password']);

Note: the attribute setting example below is not supported by driver.

$DB = new PDO($params['dbdriver'].':host='.$params['hostname'].'; dbname='.$params['database'], $params['username'], $params['password'], array(
    PDO::ATTR_PERSISTENT => false
));

Message: PDO::__construct(): SQLSTATE[IM001]: Driver does not support this function: driver does not support setting attributes

$DB = new PDO($params['dbdriver'].':host='.$params['hostname'].'; dbname='.$params['database'], $params['username'], $params['password']);

PDO::__construct(): SQLSTATE[IM001]: Driver does not support this function: driver does not support setting attributes.

Perhaps persistent links are causing the problem? I think there are non-persistent mssql_connect() functions also being used on this server. Perhaps someone can suggest a better configuration for the FreeTDS driver?

Laravel Testing (Added 08/14/2014):

I decided to test Laravel on the "same server" with the same database connection settings. It does not produce the same problem. I can hit it multiple times in a row without "hanging" or DB errors. So, this bring up the question: How is Laravel doing PHP PDO differently?

  • 写回答

2条回答 默认 最新

  • doulong4169 2014-08-14 14:04
    关注

    The Cickles PDO changes for CodeIgniter includes a bug that I found. The DB function is called multiple times, so the database connection was being called multiple times (9). In order to prevent this, I first check a reference to a global DB variable to make sure I'm not creating another connection to the database. I hope this answer helps someone else who might experience the same issue.

    See Github Project:

    https://github.com/cickes/PDOinCodeigniter2

    File:

    https://github.com/cickes/PDOinCodeigniter2/blob/master/system/database/DB.php

    if (!empty($GLOBALS['DB'])) $DB = $GLOBALS['DB'];
    //var_dump($DB);
    //echo "DB Function Called."."<br />";
    
    if (empty($DB)) {
    
    // Using PDO: connect to a database using PDO
    try {
    
        echo "Creating new PDO Connection!";
        $DB = new PDO($params['dbdriver'].':host='.$params['hostname'].'; dbname='.$params['database'], $params['username'], $params['password']);
    
        if (ENVIRONMENT == 'development') {
            $DB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        } elseif (ENVIRONMENT == 'production') {
            $DB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
        }
        $DB->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL);
        $DB->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_NATURAL);
        $DB->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
        //$DB->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    
        $GLOBALS['DB'] = $DB;
    
    } catch(PDOException $e) {
    
        //print_r($params);
        echo $e->getMessage();
    }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄