When you click a given link, it loads a page that makes a stored procedure call. If the stored procedure call takes more than a few seconds, people start clicking the link again. If they click the link a dozen times, I can see a dozen queries run on the database. The last query is the only one that returns anything to the user, but the other 11 execute to completion while using resources.
What is the best way to prevent this? Is there a way to stop it at the database level or is t开发者_JAVA百科his an interface issue? Is this a result of how I am making my database connections with PHP?
Personally I would tackle the interface issue - the reason behind users clicking multiple times is not that the users are wrong, but rather that your site isn't communicating anything about what it's trying to do. Consider setting the link to disabled onclick, perhaps displaying some kind of loading animation, and then re-enabling the link once you get the callback with the data. That way the UI reflects the back-end state at all times, and also prevents users queuing up a bunch of requests!
One solution could be to disable the link for the duration of the query. You could do this with some local JavaScript that updates the current page or switch to another page that doesn't have the link on it at all - it all depends on your site design.
Another alternative would be to set up a guard variable in the code so that it only calls the stored procedure when it's unset:
if (!callingProcedure)
{
callingProcedure = true;
call procedure
}
Then when the procedure returns reset callingProcedure
to false.
Having some sort of progress indicator will also help - giving your users feedback that the site is actually doing something.
use something like the jQuery BlockUI Plugin to block the page while the request is processed. This gives your users the certainty that something is happening and prevents them from clicking again.
精彩评论