What are the disadvantages of using persistent connection in PDO

前端 未结 8 1286
囚心锁ツ
囚心锁ツ 2020-11-22 14:41

In PDO, a connection can be made persistent using the PDO::ATTR_PERSISTENT attribute. According to the php manual -

Persistent connectio

8条回答
  •  再見小時候
    2020-11-22 15:35

    On my tests I had a connection time of over a second to my localhost, thus assuming I should use a persistent connection. Further tests showed it was a problem with 'localhost':

    Test results in seconds (measured by php microtime):

    • hosted web: connectDB: 0.0038912296295166
    • localhost: connectDB: 1.0214691162109 (over one second: do not use localhost!)
    • 127.0.0.1: connectDB: 0.00097203254699707

    Interestingly: The following code is just as fast as using 127.0.0.1:

    $host = gethostbyname('localhost');
    // echo "

    $host

    "; $db = new PDO("mysql:host=$host;dbname=" . DATABASE . ';charset=utf8', $username, $password, array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));

提交回复
热议问题