Extended Operations on ColdFusion Sessions

by @jehiah on 2005-05-20 22:03
Filed under: All, ColdFusion

This article overviews a number of undocumented functions which can be performed on ColdFusion sessions to extend their usage.

Background

The original problem I was trying to tackle was finding a way to manually expire a session in order to get ColdFusion to reclaim that memory space.

The ColdFusion Documentation recomends invalidating a session using:

[cfm]
<cfscript>getPageContext().getSession().invalidate();</cfscript>
[/cfm]

This does invalidate a session so future pages requests with that session ID throw errors when accessing the session scope, but it does not timeout that session so the memory is reclaimed. Data put in the session scope continues to persist untill the sessiontimeout attribute in the <cfapplication> tag.

Further, when a new request is made for a previously invalidated session the browser gets an ugly message instead of a new session. Using setMaxInactiveInterval() below solves that to!

There are a number of other java methods which can be performed on the ColdFusion session, so lets take a look.

getSessionCount()

Ever want to know how many sessions are currently active? You can get that data from coldfusion.runtime.SessionTracker.

[cfm]
<cfset tracker = createObject("java","coldfusion.runtime.SessionTracker")>
<cfoutput><p>There are #tracker.getSessionCount()# active sessions</p></cfoutput>
[/cfm]

getSessionCollection()

If it isn't enough to know how many sessions there are, and you want to know what data is stored in the session scope on EVERY active session on the server... you can do that to.

[cfm]
<cfset tracker = createObject("java","coldfusion.runtime.SessionTracker")>
<cfset sessions = tracker.getSessionCollection(application.applicationName)>
<cfdump var="#sessions#">
[/cfm]

setMaxInactiveInterval()

To change the timeout value of the current session from the value specified in the <cfapplication> tag, use the setMaxInactiveInterval() funcation. This function takes a value which is the number of seconds to allow before reclaiming that session. Interestingly this does not work when used against the getPageContext().getSession() object even though you would expect it to. A paramater of 1 will effectivly emulate the suggested invalidate() function by reclaiming the session after 1 second of inactivity. (note: there may be a slight delay between when that session is inactive and when ColdFusion checks it's inactivity to reclaim it. The session is still active during that time.) To set the current session to timeout after 5 minutes we would run:

[cfm]
<cfscript>session.setMaxInactiveInterval(280);</cfscript>
[/cfm]

getTimeSinceLastAccess()

To find the number of seconds a session has been inactive use:

[cfm]
<cfoutput><p>Last request was #session.getTimeSinceLastAccess()# seconds ago</p></cfoutput>
[/cfm]

If you want to see all this data for every session we need to go about this in a more round about way. If we simply looped through all the sessions above and checked the time since last access, that last access for each session would be updated (not good). To work around this as mentioned on rewindlife we need to use reflection.

[cfm]
<cfset tracker = createObject("java","coldfusion.runtime.SessionTracker")>
<cfset sessions = tracker.getSessionCollection(application.applicationName)>
<cfscript>
a = ArrayNew(1);
sessionClass = a.getClass.forName("coldfusion.runtime.SessionScope");
getTimeSinceLastAccessMethod = sessionClass.getMethod("getTimeSinceLastAcccess",a);
</cfscript>
<cfloop item="s" collection="#sessions#">
    <cfoutput>#s# - #getTimeSinceLastAccessMethod(sessions[s],a)#<br></cfoutput>
</cfloop>
[/cfm]
Subscribe via RSS ı Email
© 2016 - Jehiah Czebotar