如何保持数据库连接处于活动状态?

发布于 2024-08-05 05:50:21 字数 135 浏览 2 评论 0原文

我正在使用 ajax,并且经常(如果不是总是)第一个请求超时。事实上,如果我在提出新请求之前延迟几分钟,我总是会遇到这个问题。但是后面的请求都是OK的。所以我猜测第一次使用的数据库连接已经死了。我正在使用MySQL。

有什么好的解决办法吗?

I'm using ajax, and quite often if not all the time, the first request is timed out. In fact, if I delay for several minutes before making a new request, I always have this issue. But the subsequent requests are all OK. So I'm guessing that the first time used a database connect that is dead. I'm using MySQL.

Any good solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

困倦 2024-08-12 05:50:21

您能否澄清一下:

  • 您是否正在尝试建立持久连接?
  • 基本的 MySQL 查询是否有效(例如,SELECT 'hard-coded' FROM DUAL
  • MySQL 查询执行 ajax 调用需要多长时间(例如,如果您从 mysql 命令行或 GUI 客户端运行它)。 )
  • 写入 AJAX 查询中使用的 MySQL 表的频率如何?

回答这些问题应该有助于排除与建立持久连接无关的其他问题:基本数据库连接、表索引/运行缓慢的 SQL、MySQL 缓存失效等。

Can you clarify:

  • are you trying to make a persistent connection?
  • do basic MySQL queries work (e.g. SELECT 'hard-coded' FROM DUAL)
  • how long does the MySQL query take for your ajax call (e.g. if you run it from a mysql command-line or GUI client.)
  • how often do you write to the MySQL tables used in your AJAX query?

Answering those questions should help rule-out other problems that have nothing to do with making a persistent connection: basic database connectivity, table indexing / slow-running SQL, MySQL cache invalidation etc.

妞丶爷亲个 2024-08-12 05:50:21

您的问题很可能不是打开连接,而是实际处理请求。
由于mysql查询缓存,后续调用速度很快。

你需要做的是寻找慢mysql查询,例如打开慢查询日志,或者使用mytop或“SHOW PROCESSLIST”实时查看服务器,看看是否有查询时间太长。如果您找到了,请使用 EXPLAIN 确保它已正确索引。

chances are that you problem is NOT opening the connection, but actually serving the request.
subsequent calls are fast because of the mysql query cache.

what you need to do is to look for slow mysql queries, for example by turning on the slow query log, or by looking at the server at real time using mytop or "SHOW PROCESSLIST" to see if there is a query that takes too long. if you found one, use EXPLAIN to make sure it's properly indexed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文