如何保持数据库连接处于活动状态?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您能否澄清一下:
SELECT 'hard-coded' FROM DUAL
)回答这些问题应该有助于排除与建立持久连接无关的其他问题:基本数据库连接、表索引/运行缓慢的 SQL、MySQL 缓存失效等。
Can you clarify:
SELECT 'hard-coded' FROM DUAL
)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.
您的问题很可能不是打开连接,而是实际处理请求。
由于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.