gpt4 book ai didi

php - PDO 连接池与 AJAX

转载 作者:行者123 更新时间:2023-11-29 01:30:39 25 4
gpt4 key购买 nike

我不知道 PDO 的连接池是如何工作的,而且我的搜索一无所获。

首先,为了清楚起见,PDO 的连接池是自动的吗?

如果没有,我如何在 AJAX 页面上使用它?

我计算了我的本地 wampserver 开发人员通过 mysqli 建立数据库连接的时间成本,每次 10 毫秒,并且我正在努力使等待时间尽可能接近 0。这是我的最后一期。

最佳答案

如果您谈论的是持久连接,请看这里:http://php.net/manual/en/pdo.connections.php

Do persistent connections remain open across different AJAX requests?

是的,而且它们永远不会被重复使用的风险。感谢@carefulnow。我将在这里引用他的评论:

Persistent connections and connection pooling are not the same thingConnection pooling creates a lot of connections to start with, and then the application just grabs one of those already established connections. Persistent connections do establish new connections, but they do not close unless explicitly told to (a normal connection will close at the end of the script). Persistent connections are dangerous if not used carefully, as most databases implement a maximum connections count, and will refuse further connections when hit. For most cases, a normal connection (just calling the PDO constructor with a DSN and login details) will be fine.

I'll explain it by showing the PHP code, and the resulting connection count in the MariaDB console monitor. Look at the following screenshots, and pay attention the the "run count" in the bottom right. You'll see when using persistent connections, connections do not close, but instead go into sleep mode. Ignore connection #231, that's the console monitor's. http://i.imgur.com/IL42tjF.png, http://i.imgur.com/aDvl7F7.png, http://i.imgur.com/IuFEEvO.png. As you can see, the time column indicates the length of inactivity, not the length of time connected, hence the console monitor's time is always zero, because I've just executed SHOW PROCESSLIST;. I hope this clears everything up a bit, database connection management is quite tricky sometimes.

因此查看屏幕截图问题是开放的,如何再次拾取这些持久连接。 StackOverflow 上的一些答案说,它们是在线程级别缓存的,因此如果您有多个线程(这是非常期望的),那么您会获得很多连接。这打开了遇到 mysql 连接限制的风险。在使用持久连接之前,确保你了解它们是如何工作的(我不知道,但看起来@carefulnow :-))

Is it possible to set a persistent connection to expire?

PHP 文档说它们已被缓存,因此我假设它们会在一段时间后被清理。

More on the php doc

关于php - PDO 连接池与 AJAX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14248043/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com