最近在swoole中使用php pdo擴展訪問數據庫的時候,發現了一個頗有意思的事情。php
我測試用的版本是 PHP 7.1.13html
官方手冊明確到告訴咱們,使用PDO是這樣關閉數據庫鏈接的,只須要將PDO對象置爲null便可,這段代碼就是手冊裏的mysql
<?php try { $dbh = new PDO('mysql:host=127.0.0.1;dbname=test', 'root', ''); foreach($dbh->query('SELECT * from test') as $row) { print_r($row); } sleep(100); $dbh = null; } catch (PDOException $e) { print "Error!: " . $e->getMessage() . "<br/>"; die(); }
我實際測下來也的確如此。 這是query執行前的MySQL的PROCESSLISTsql
mysql> SHOW PROCESSLIST; +----+------+-----------------+------+---------+------+----------+------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------------+------+---------+------+----------+------------------+ | 10 | root | localhost:56477 | NULL | Query | 0 | starting | SHOW PROCESSLIST | +----+------+-----------------+------+---------+------+----------+------------------+ 1 row in set (0.00 sec)
這是query運行後,PDO 對象置爲null前的MySQL的PROCESSLIST數據庫
mysql> SHOW PROCESSLIST; +----+------+-----------------+------+---------+------+----------+------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------------+------+---------+------+----------+------------------+ | 10 | root | localhost:56477 | NULL | Query | 0 | starting | SHOW PROCESSLIST | | 14 | root | localhost:56572 | test | Sleep | 2 | | NULL | +----+------+-----------------+------+---------+------+----------+------------------+ 2 rows in set (0.00 sec)
PDO 對象置爲null後,數據庫鏈接的的確確關閉了,看來官方手冊沒錯swoole
mysql> SHOW PROCESSLIST; +----+------+-----------------+------+---------+------+----------+------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------------+------+---------+------+----------+------------------+ | 10 | root | localhost:56477 | NULL | Query | 0 | starting | SHOW PROCESSLIST | +----+------+-----------------+------+---------+------+----------+------------------+ 1 row in set (0.00 sec)
可是接下來有意思的事情就要發生了,看這段代碼架構
<?php $pdo = new PDO( 'mysql:dbname=test;host=127.0.0.1;port=3306;charset=utf8mb4', 'root', '', [ PDO::ATTR_TIMEOUT => 10, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_PERSISTENT => false, PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC ] ); $s = $pdo->prepare("select * from test WHERE id=:id"); $s->execute(['id' => 1]); $s->fetchAll(); $pdo = null; sleep(100);
在PDO對象被置爲null以後,MySQL的鏈接並無被真的釋放測試
mysql> SHOW PROCESSLIST; +----+------+-----------------+------+---------+------+----------+------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------------+------+---------+------+----------+------------------+ | 10 | root | localhost:56477 | NULL | Query | 0 | starting | SHOW PROCESSLIST | | 18 | root | localhost:56606 | test | Sleep | 3 | | NULL | +----+------+-----------------+------+---------+------+----------+------------------+ 2 rows in set (0.00 sec)
是否是很詭異?不要緊,咱們在改下這段代碼fetch
<?php $pdo = new PDO( 'mysql:dbname=test;host=127.0.0.1;port=3306;charset=utf8mb4', 'root', '', [ PDO::ATTR_TIMEOUT => 10, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_PERSISTENT => false, PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC ] ); $s = $pdo->prepare("select * from test WHERE id=:id"); $s->execute(['id' => 1]); $s->fetchAll(); $s=null; $pdo = null; sleep(100);
在$s也被置爲null以後將$pdo置爲null,pdo申請的數據庫鏈接就被真真正正的釋放了.net
mysql> SHOW PROCESSLIST; +----+------+-----------------+------+---------+------+----------+------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------------+------+---------+------+----------+------------------+ | 10 | root | localhost:56477 | NULL | Query | 0 | starting | SHOW PROCESSLIST | +----+------+-----------------+------+---------+------+----------+------------------+ 1 row in set (0.00 sec)
這是爲何?看下面這段代碼
$pdo = new PDO( 'mysql:dbname=test;host=127.0.0.1;port=3306;charset=utf8mb4', 'root', '', [ PDO::ATTR_TIMEOUT => 10, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_PERSISTENT => false, PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC ] ); debug_zval_dump($pdo); $s = $pdo->prepare("select * from test WHERE id=:id"); debug_zval_dump($pdo); $s->execute(['id' => 1]); $s->fetchAll(); $s=null; debug_zval_dump($pdo); $pdo = null; sleep(100);
輸出
object(PDO)#1 (0) refcount(2){ } object(PDO)#1 (0) refcount(3){ } object(PDO)#1 (0) refcount(2){ }
你會發現,當$pdo對象第一次打印的時候他的引用計數是2(爲何是2,而不是1呢?傳送門 http://php.net/manual/zh/function.debug-zval-dump.php )在$pdo->prepare以後引用計數變成了3,也就是說,即便$pdo被置爲null,它依然仍是會被$s引用,並不會被PHP的GC給回收掉,因此它建立的數據庫鏈接也不會被釋放。
在FPM模式下的PHP,這個問題並不會引發什麼問題,可是在SWOOLE這種運行環境下,假如你的PDO對象在被一些全局變量(傳送門 https://wiki.swoole.com/wiki/page/p-zend_mm.html ) 引用,即便你將PDO對象置爲null,你的數據庫鏈接依然不會被釋放。
更多架構、PHP、GO相關踩坑實踐技巧請關注個人公衆號:PHP架構師