Application and Session Variables

Application variables are a great way to store site-wide variables that rarely need to change and Session variables are essential in storing data for a user regardless of where they are in your site. However, when not used properly, both types of variables can cause problems.

When the Application and Session scopes are not "locked" for variable reads and writes, memory or variable conflicts can occur. But who wants to use the CFLOCK tag every time you call #Application.dsn#? Also, Application variables that are reset each time Application.cfm is run are very inefficient. Why use Application variables when they are reset each time a page loads?

The following methods can help ensure that you are only setting variables when you need to and that you'll need to use CFLOCK a lot less.

Application.cfm

Since Application.cfm is accessed for every ColdFusion page call, you want to ensure that your Application variables are only being set/reset when they need to be; when they don't already exist. By setting a default value to Application.Initialized you can determine whether or not you need to set your variables:

<!--- Create an exclusive lock for the Application scope and
   set a default value for Application.Initialized. --->
<cflock scope="Application" timeout="10" type="exclusive">
  <cfparam name="Application.Initialized" default="false">
  <!--- Create a Request variable to hold the value so you don't
    need a read lock to get it in the next block of code. --->
  <cfset Request.Initialized = Application.Initialized>
</cflock>

<!--- If Request.Initialized is false, then the Application scope
   has been reset and the variables need to be set. --->
<cfif NOT Request.Initialized>
  <!--- Create an exclusive Application scope lock and set the values. --->
  <cflock scope="Application" timeout="10" type="exclusive">
    <cfscript>
      Application.Initialized = true;
      Application.DS = "Datasource Name";
      
      ...
    </cfscript>
  </cflock>  
</cfif>

The same method can be used to set default Session variables for a user, just change the Application scope to Session scope in the locks and variable sets.

Once you use Application and Session variables, you should also perform locks each time you read or access one of these variables. This can be cumbersome and inefficient. A much easier method is to duplicate the scopes into Request variables. If this is done at the bottom of Application.cfm, the copy is made each time a page is called. The Request scope is used because Request variables are available to all templates that are called within a page, such as pages called using CFINCLUDE and CFMODULE and CFCs. The Duplicate() function is used because it will also copy complex variables such as structures and query result sets.

<!--- Create a read-only lock for the Application scope
   and duplicate it into Request.App. --->
<cflock scope="Application" timeout="10" type="readonly">
  <cfset Request.App = Duplicate(Application)>
</cflock>

<!--- Create a read-only lock for the Session scope
   and duplicate it into Request.Ses. --->
<cflock scope="Session" timeout="10" type="readonly">
  <cfset Request.Ses = Duplicate(Session)>
</cflock>

Reading Scoped Variables

Now that the scoped variables have been copied into Request variables, simply replace Application or Session with Request.App and Request.Ses respectively to access them.

#Request.App.DS#
#Request.App.FilePath#

#Request.Ses.UserID#
#Request.Ses.ShoppingCartQuerySet.RecordCount#

The Request variables will be refreshed each time a page is called and Application.cfm is run. Because the Duplicate() function is quick and efficient, there is no drag on the application.

Updating Scoped Variables

Because the Request variables are updated only when Application.cfm exists, the following code may cause an error, or may not return the expected value:

<cflock scope="Session" timeout="10" type="exlusive">
   <cfset Session.UserName = GetUserName.UserName>
</cflock>
<cfoutput>#Request.Ses.UserName#</cfoutput>

To update the Request variables, you can either set them individually or duplicate the scope again:

<cflock scope="Session" timeout="10" type="exlusive">
   <cfset Session.UserName = GetUserName.UserName>
   <cfset Request.Ses.UserName = Session.UserName>
</cflock>
<cfoutput>#Request.Ses.UserName#</cfoutput>

<cflock scope="Session" timeout="10" type="exlusive">
   <cfset Session.UserName = GetUserName.UserName>
   <cfset Request.Ses = Duplicate(Session)>
</cflock>
<cfoutput>#Request.Ses.UserName#</cfoutput>

The second method is more efficient if you are setting several variables at once.

Resetting Values

There may be times when you want to easily reset your Application or Session variables without changing your code or resetting ColdFusion. The following code can be used run to reset the scopes easily:

<!--- Create an exclusive lock for the Application scope
   and set Application.Initialized to false. --->
<cflock scope="Application" timeout="10" type="exclusive">
  <cfset Application.Initialized = false>
</cflock>

<!--- Create an exlusive lock for the Session scope
   and clear the scope. --->
<cflock scope="Scope" timeout="10" type="exclusive">
  <cfscript>
    StructClear(Session);
  </cfscript>
</cflock>

Join Our E-Mail Newsletter

For Email Newsletters you can trust


Sister Sites

RiteTech LLC
Modern HOA