Chad Sollis wrote:
Greetings,

I have a related problem to my original mysql locking issue that I want to see if I can optimize. I have my own session management object that I use to manage sessions in a load balanced environment. I feel that it could probably be optimized (and perhaps reduce my table locking issues as well).

The code for the session object is here (expire in 2 days):

http://pastebin.ca/658639 ...
Hi Chad,

I recently also had to implement sessions in a database. Our situation was a load-balanced application that served customers using a reverse proxy (the user's ip address could change at any moment--ugh!).

I actually got a couple of improvement ideas based on your last thread:

1. Do not garbage collect on every request. Basically, you can add a time check against sessions.ses_date_modified in the where clause of the _read() function query and then do garbage collection from a periodic cron script.

2. Don't lock the table. The majority of your application is probably not subject to race conditions in the sessions. Instead of locking the session every time, try an explicit lock when you need it. The locking doesn't even have to be session-specific. You can use the mysql functions GET_LOCK(), IS_USED_LOCK(), and RELEASE_LOCK() (http://dev.mysql.com/doc/refman/4.1/en/miscellaneous-functions.html). Basically what you do here is add some functions to your session class:

SessionManager::aquireLock($sessionVariable);
// this would send a query with 'IS_USED_LOCK(sess_' . session_id() . '_' . $sessionVariable .')' // if used, the script would wait, retry, and call GET_LOCK() once the lock was free

/* do race-condition sensitive operations here */

SessionManager::releaseLock($sessionVariable);
// this would call SessionManager::_write() and then send a query with RELEASE_LOCK()


The nice thing about mysql locks is that they automatically release when a client disconnects so even errored scripts will release locks. The mysql locks can be used for just about anything because they only depend on acquiring and releasing with the same naming convention--any name you wish.

Here is a really excellent article with code for 6 different types of session management: http://thwartedefforts.org/2006/11/11/race-conditions-with-ajax-and-php-sessions/

- Ken Snyder

_______________________________________________

UPHPU mailing list
[email protected]
http://uphpu.org/mailman/listinfo/uphpu
IRC: #uphpu on irc.freenode.net

Reply via email to