A really interesting quirk with AJAX & mySQL « home
posted on 12:27 - 15 August 2009 | posted by Lev
last modified on 20:56 - 30 August 2009 | last modified by Lev
This is now the second time I have encountered this behavior with working with AJAX; this time it is with the "my status" module in Theia.
Consider this scenario:
Everything added or removed through the "my status" module is AJAX based; that is, a silent request is made through javascript instead of reloading the whole page.
When updates are added or deleted, the list of updates on the page is told to reload immediately (instead of waiting for the next refresh time).
So, when someone adds a message, this happens:
- a javascript function is invoked which sends the POST data to an ajax routine, which then adds the record to the mySQL database
- after the initial function (for adding the record) is complete, another javascript function is invoked to instantly reload the update divs.
All of this happens within a fraction of a second.
What you would probably expect to happen is, the message is added through the first function call, and upon the second, you should see the most up to date version of the updates on the page as the second function triggers this.
What *really* happens is a bit differently. After a lot of experimenting with this, I've come to conclude that there is a probable chance the updates div will have refreshed properly, and there is a probable chance that the updates div will not list your most recent update.
Why???
Well, I'm not 100% certain, but I theorize that this is because it's all happening to quickly. Even though programming code occurs chronologically (in other words, whichever code appears first gets run first), the interval between when all of this is happening is so incredibly minuscule, that by the time the updates div is being told to refresh, it isn't even aware of the new record! Kinda amazing...
Oddly enough, I am not using persistent connections in mySQL. I have good enough evidence to suspect it could be caused by mySQL's query cache (maybe it takes a little more time until the query cache is purged of stale data).
I've only come up with one effective workaround so far: Delay execution of displaying the status updates.. What I did at first is used PHP's usleep function and added a half second delay from when the status updates ajax routine is called, and incredibly this seems to have completely solved the issue. You could also set a timeout in your AJAX function itself.
I hope this helps shed a little light on why some other developer out there may be experiencing the same sort of behavior.
Consider this scenario:
Everything added or removed through the "my status" module is AJAX based; that is, a silent request is made through javascript instead of reloading the whole page.
When updates are added or deleted, the list of updates on the page is told to reload immediately (instead of waiting for the next refresh time).
So, when someone adds a message, this happens:
- a javascript function is invoked which sends the POST data to an ajax routine, which then adds the record to the mySQL database
- after the initial function (for adding the record) is complete, another javascript function is invoked to instantly reload the update divs.
All of this happens within a fraction of a second.
What you would probably expect to happen is, the message is added through the first function call, and upon the second, you should see the most up to date version of the updates on the page as the second function triggers this.
What *really* happens is a bit differently. After a lot of experimenting with this, I've come to conclude that there is a probable chance the updates div will have refreshed properly, and there is a probable chance that the updates div will not list your most recent update.
Why???
Well, I'm not 100% certain, but I theorize that this is because it's all happening to quickly. Even though programming code occurs chronologically (in other words, whichever code appears first gets run first), the interval between when all of this is happening is so incredibly minuscule, that by the time the updates div is being told to refresh, it isn't even aware of the new record! Kinda amazing...
Oddly enough, I am not using persistent connections in mySQL. I have good enough evidence to suspect it could be caused by mySQL's query cache (maybe it takes a little more time until the query cache is purged of stale data).
I've only come up with one effective workaround so far: Delay execution of displaying the status updates.. What I did at first is used PHP's usleep function and added a half second delay from when the status updates ajax routine is called, and incredibly this seems to have completely solved the issue. You could also set a timeout in your AJAX function itself.
I hope this helps shed a little light on why some other developer out there may be experiencing the same sort of behavior.
Related Items »






