I'm using transactions in a MySQL database, along with Ajax, and I'm having some troubles with scripts timing out. So, here's a rundown of what happens:
- Ajax script on page makes request to server.
- Server receives request; script starts a MySQL transaction.
- Ajax script is set to timeout after two seconds; times out; tells server to abort request.
- Server script dies; transaction is left hanging.
- MySQL notices transaction has been left hanging; rolls back as it's supposed to, but not before a bunch of customers become less than happy because the tables were locked for half a minute and they couldn't access the website.
What to do?
I imagine the server script just needs a bit more time than the two seconds it's given, so I called ignore_user_abort()
on every page, and then called register_shutdown_function()
to explicitly roll back a transaction if the client has in fact aborted.
Is this a good plan? Is there an alternative?