views:

107

answers:

2

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?

A: 

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.

Omry
But the only query that's got executed is :mysql_query("BEGIN", $con);I terminated it from testing purpose.
Shore
is it a shared hosting server?
Omry
yes it is a shared hosting server
I'm testing it from localhost.
Shore
do you know that browsers limit the number of concurrent open connections to a single web server?try to call your server side function directly and see how long it normally takes (also for the 'first' call).
Omry
+1  A: 

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.

searlea