CF Application and Session Scopes

On October 20, 2007, in ColdFusion, by Anuj Gakhar

Accessing Application and Session Scope in Coldfusion is generally straightforward but here is a rather smart way of doing it.The first step is to acces the CF runtime objects like below :-

<cfset appTracker = createObject(“java”,“coldfusion.runtime.ApplicationScopeTracker”)/>
<cfset sessionTracker = createObject(“java”,“coldfusion.runtime.SessionTracker”)/>

And this is how you get all the existing applications in CF runtime.

<cfset apps = appTracker.getApplicationKeys() />
<cfset appsList = “”/>
<cfloop condition=“#apps.hasMoreElements()#”>
<cfset appName = apps.nextElement().toString()/>
<cfset appsList = listAppend(appsList, appName)/>
<cfset appsList = listSort(appsList, ‘textNoCase’, ‘asc’)/>
<cfdump var = “#appsList#”>

To access a particular Application scope :-

<cfset appName = appTracker.getApplicationScope("mysite") />
<cfdump var = "#appName#">

Similarly, to get all existing Session keys :-

<cfset sessions = sessionTracker.getSessionKeys() />
<cfset sessionList = “” />
<cfloop condition=“#sessions.hasMoreElements()#”>
<cfset sessionList = listAppend(sessionList,sessions.nextElement())/>
<cfdump var = “#sessionList#”>

To get a Session Collection for a particular application :-

<cfdump var = "#sessionTracker.getSessionCollection("mysite")#"/>

To programmatically clear an application scope :-

<cfset myApp = createObject('java','coldfusion.runtime.ApplicationScopeTracker').init() />
<cfset myApp.getApplicationScope(mysite).cleanup() />
Tagged with:  

8 Responses to CF Application and Session Scopes

  1. andy says:

    For what version of CF is this code intended? I’ve got MX7.

  2. Anuj Gakhar says:

    Andy, This should work on MX7. Did you try it ?

  3. andy says:

    Yes! Thank you. Does this open any security holes?

  4. Anuj Gakhar says:

    Depends how you want to use it 🙂

  5. Anuj Gakhar says:

    Bear in mind, this wont work if the “Disable access to internal ColdFusion Java components ” is checked in CF adminstrator.

  6. Hi Anuj,

    I’m investigating a performance problem with our application and have seen this code (or very similar) used to count user sessions. Can you tell me if you think this code will effectively “keep-alive” the sessions retrieved from the tracker. The code is also calling methods to retrieve objects from the sessions rertieved from the tracker.

  7. Anuj Gakhar says:

    Hi Rob,

    I have not used this code for keeping alive a session. But you shouldn’t have to use this code to count user sessions. This code is more of a server level thing to look at all the sessions and applications in a server instance.

    What is the performance issue you facing?

  8. Shirak says:

    This is wonderful couple line of code. Clearing application scope is working, I’m trying to clear session scope for specific application, I tried cleanup() and remove() but with no success, can you give a little guide how to clear all sessions?

Leave a Reply

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 445 other subscribers

© 2011 Anuj Gakhar
%d bloggers like this: