A Scalable Alternative to Session Variables, Part 3By Scott Sargent
In Part 2 we looked at the stored procedures needed for the GUID Session
application. In this part, we will examine the needed ASP code, starting with
a file you'll need to include in all of your ASP pages that use the GUID Session approach.
guidsession.asp contains a function called
newsession that obtains a new GUID for a
newsession function that goes to the database & gets a GUID for our user for the very first
time. To call this function simply do this
sessionid = newsession() Easy enough. Most of the hard work is
done by the stored procedure in this one. This function calls the newsession stored procedure.
checkqs function saves our butt if we forget to pass the GUID to some page.
This looks for the GUID; if it cannot find it it goes to the database to find it. if this is still
unsuccessful it creates a new one & returns it to the calling function. One thing to remember here, is that
although this makes it easier as we do not have to remember to pass the GUID, if we do not its an extra
database call. This function calls the
Retrieve_GUID stored procedure if the GUID isn't in the
This function is the one we've been waiting for; this is how we store the state information or
GUIDSession variables. The most complicated part of this is again with the stored procedure. To
call this function all you do is this:
input_session id, "USERNAME", "MWITTMAN" if you want to
update a value, say set USERNAME to MAPPLEBY, all you do is call this function with the updated data. The
stored procedure will check & see if the variable already exists, if it does it will simply update it.
retrieve_session function retrieves the information back out of the database; for example,
retrieve_session(sessionid, "USERNAME") would return MWITTMAN as per our earlier example. This
is the simplest of all our functions.
Well that's the
GUIDSession method, or a Scalable Alternative to Session Variables. Just a few
notes about it, for simplicity I have written all of the functions in vbscript & asp, to really get the maximum
performance out of this method an MTS Object would work best. In the future I will be working on a VB MTS
Component and then perhaps an ATL COM MTS (wish me luck). Another benefit of this method is that you can use
this inside of other components or programs. That way you can maintain state across multiple platforms & programming
Also this is a work in progress; if anyone has any suggestions or questions upon how to improve it please feel free to email me. My inspiration for writing this article was that we all could find a better way of maintaining state. I know this isn't a finished program/technique, but I believe it's a very good start.
guidsession.aspin text format