2

在多个 PHP 页面之间传输和/或共享资源链接是否有任何技巧?我不希望服务器继续连接到数据库只是为了获得另一个链接到同一个数据库中的同一个用户。

请记住,从 mysql_connect() 返回的链接在它源自的脚本完成执行后会自动关闭。无论如何在每个会话结束时手动关闭它吗?

4

2 回答 2

4

PHP 允许持久的 mysql 连接,但也有缺点。最重要的是,空闲的 apache 孩子最终会坐在那里,保持空闲的数据库连接处于打开状态。数据库连接会占用大量内存,因此您真的只希望它们在实际使用时打开。

如果您的用户每分钟打开一个页面,那么在您不使用它的每一分钟中关闭数据库连接 59 秒,并在需要时重新打开它,而不是持续打开它。

相反,您可能应该研究连接池。

于 2009-03-30T19:40:24.800 回答
0

One of the advantages of Mysql over other heavier-weight database servers is that connections are cheap and quick to setup.

If you are concerned about large numbers of connections to retrieve the save information, you may like to look at such things as caching of information instead, or as well as getting the information from disk. As usual, profiling of the number, and type of calls to SQL that are being made, will tell you are great deal more than anyone here guessing at what you should really be doing next.

于 2009-03-30T19:58:37.660 回答