mysql_connect後是不是非要mysql_close,是不是自動釋放,該如何解決

來源:互聯網
上載者:User
mysql_connect後是不是非要mysql_close,是不是自動釋放
大傢伙們,PHP資料庫連接是不是在頁面執行完之後自己釋放,即使不用mysql_close()也可以釋放掉?
同理PDO串連到資料庫後,是不是非要將PDO執行個體賦值為NULL,才能將PDO串連斷掉?

在N久以前我查過資料,但己經模糊不清了,具說PHP處理每一個被請求的頁面時,裡面有MYSQL串連的,當頁面處理完就立刻釋放掉所有用到的資料。如:mysql_connect後的串連。這個說法對嗎?PDO同理嗎?

------解決方案--------------------
以下摘自PHP手冊:
mysql_close() closes the non-persistent connection to the MySQL server that's associated with the specified link identifier. If link_identifier isn't specified, the last opened link is used.

Using mysql_close() isn't usually necessary, as non-persistent open links are automatically closed at the end of the script's execution. See also freeing resources.

mysql_close對於非永久的connect不是必須的。
------解決方案--------------------
mysql_connect不一定需要強制用mysql_close關閉的
可以去看下php手冊
上面說了mysql_connect() 函數開啟非持久的 MySQL 串連。注釋:指令碼一結束,到伺服器的串連就被關閉,除非之前已經明確調用 mysql_close() 關閉了。
也就是說,當指令碼結束時,會自動關閉。
------解決方案--------------------
如果是mysql_pconnect就不一樣了,需要自己用mysql_close關閉
------解決方案--------------------
通常不需要使用 mysql_close(),因為已開啟的非持久串連會在指令碼執行完畢後自動關閉,
------解決方案--------------------
探討

如果是mysql_pconnect就不一樣了,需要自己用mysql_close關閉

------解決方案--------------------
那mysql_connect開啟的串連會在指令碼執行完畢後由SQL伺服器自動關閉嘍?

這個是的

長鏈接的默認時效是8個小時.8個小時內你沒有操作那麼將斷開你的鏈接

wait_timeout=28800
------解決方案--------------------
CLI模式下,不論是mysql_connect還是mysql_pconnect,只要指令碼退出,串連就被關閉。

CGI模式下,mysql_connect建立的串連,可以被mysql_close關閉;也可以不手工關閉,而等待請求結束後自動釋放。但是mysql_pconnect建立的串連,無法被mysql_close關閉,請求結束後也不會釋放。

mysql_pconnect被關閉的情況,我知道有:
1、空閑時間超過了mysqld的wait_timeout(單位是秒)
2、web server進程退出,以nginx+php-fpm為例,就是一個php-fpm進程結束
------解決方案--------------------
手冊上說的比較權威

http://cn.php.net/manual/zh/pdo.connections.php

Upon successful connection to the database, an instance of the PDO class is returned to your script. The connection remains active for the lifetime of that PDO object. To close the connection, you need to destroy the object by ensuring that all remaining references to it are deleted--you do this by assigning NULL to the variable that holds the object. If you don't do this explicitly, PHP will automatically close the connection when your script ends.
------解決方案--------------------
去年無聊時研究過mysql_pconnect,認真寫了篇博文,你可以看看
http://www.cnblogs.com/funlake/archive/2011/09/08/2171822.html
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.