php - PDO 连接池与 AJAX

标签 php mysql time pdo connection-pooling

我不知道 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 thing Connection 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/

相关文章:

php - 如何使用 Ajax 和 PHP 从动态文本框中插入数据

java - `serverTimezone` 参数是否更改 MySQL 中的@@session.time_zone?

php - Amazon S3 在创建对象时抛出 cURL 错误

php - drupal view2用于JOIN的views_query_alter

javascript - 如何使用 php 从 mysql 检索数据并将其显示在 javascript 上?

PHP 和 JavaScript - "Logout"代码在 Chrome 中有效,但在 Firefox 中无效

mysql - 我想在我的 mysql 表中启用显示或隐藏

java - java中的时间操纵/模拟和事件调度

c++ - ofstream的时间可以忽略不计

c - 将 time_t 设置为毫秒