tags:

views:

170

answers:

2

I'm looking to change my application to store PHP Session data in MySQL. The application is ajax intensive but does not utilize long-polling or any other type of persistent connection.

My question is, are there any gotcha's I need to be aware of when writing this code? For example; I have a page that sends 4 ajax requests to load data (for another topic); will this cause any locking issues if I have to update the session data on every request?

Thanks.

+1  A: 

There could be race conditions if you make ajax requests to multiple scripts at once. You can't verify they will complete in the order you assigned the ajax requests unless you use callbacks, which would prove quite a bit slower.

It should be fine as long as you're only reading, but if you're writing when multiple requests are being made you have no way to verify that the reads are taking place on the most recent data.

Ian Elliott
OK, not so bad, so is this correct?- I need to store the session data once the user logs in- I need to read the session data on every request- I need to delete the session data once the user logs out.Is that the correct process flow? What happens if he doesn't log out? How will I know when to update the session data?
frio80
Update the session only when you need to, logout/login are the major ones. If a user doesn't log out it should be fine to keep the session stored, but include a time-stamp when it was last stored so you can do cleanups if people start a session but never logout/come back.
Ian Elliott
+1  A: 

You would be a lot better off storing session data in Memcache if speed is your primary concern. You won't have the database latency related with storing data in tables--memcache uses hash keys to store data in RAM. This frees up your relational database to do actual relational queries and frees up memory for table caching other parts of the database (the many requests to the session table will fill up any available memory for caching other tables, otherwise it will be convinced that the session cache is of the utmost importance).

Sure, it's another dependency and service to learn and maintain, but the speed improvement is immense.

Nolte Burke
About the Memcache extension:http://us2.php.net/memcachePlenty of articles about using Memcache to store your session data:http://www.google.com/search?q=php+memcache+sessionsAlso, once you have this object database setup you will be able to use it in other parts of your problem to improve the look up of individual data records without having the burden and overhead of the relational database.
Nolte Burke