Application Variables


Application variables are a special variable type that can be used within ColdFusion applications. Application variables could be compared with global variables in other types of application programming because after an application variable is created, it is accessible to all users of the application.

Application variables are intended to define variables and settings that remain constant throughout your entire application. It is common for datasource names to be set to the application scope.

Application variables must be referred to using the application prefix. An application variable looks like this:

 <cflock scope="application" type="exclusive" timeout="10">      <cfset application.DSN="ICFMX">  </cflock>  

CFAPPLICATION Tag

To make use of application scoped variables, you must place a call to the CFAPPLICATION tag. By calling this tag, you initialize and name the application and have the capability to define other aspects of the application as well.

The CFAPPLICATION tag is typically called within the Application.cfm.It initializes the application and sets the application name. It also defines the scoping of variables within the application, enables the storage of client variables, and sets a location for the storage of those variables.

The CFAPPLICATION tag also enables the use of session variables in your ColdFusion application. Until you've enabled the use of application and session variable scopes within the CFAPPLICATION tag, you cannot use them within your application. Not only do you enable the use of application- and session-scoped variables within the CFAPPLICATION tag, but also you use it to set their timeout period.

 <cfapplication      name="InsideCFMX"      sessionmanagement="Yes"      sessiontimeout="#CreateTimeSpan("0,0,60,0")#"> 

The CreateTimeSpan() function is used to specify the timeout for the session variables within this application.

Using Application Variables

Now that you've enabled the use of application variables in your application, you can begin to use them. Because application variables are global to your application after they are set, your code should never assume that they have or have not been set. You should always test for the existence of an application variable prior to setting it. The reason is that you don't want to unnecessarily set the value of any variable that is stored in memory.

 <cflock scope="application" type="exclusive" timeout="10">    <cfparam name="application.dsn" default="insidecfmx">  </cflock> 

You might have also seen the same functionality in a CFIF statement. CFPARAM is cleaner and more performance-efficient.

 <cflock scope="application" type="exclusive" timeout="10">      <cfif NOT IsDefined("application.dsn")>        <cfset application.dsn="insidecfmx">      </cfif>  </cflock> 

Application variables, such as a datasource name, might be used over and over throughout your application. They might also be used on the same template in several places. Because application variables are shared memory variables, it makes sense to convert these variables to another variable scope prior to using them in our templates. There are two reasons for this:

  • We want to avoid the practice of wrapping a CFLOCK tag around more than just a variable access.

    If we call an application-scoped variable as our datasource, we might find ourselves wrapping the CFLOCK around the entire query:

     <cflock scope="application" type="readonly" timeout="10">  <cfquery name="AuthenticateUser" datasource=application.dsn>  SELECT UserID        FROM Users        WHERE Email = '#form.email#'                AND Password = '#form.password#'  </cfquery>  </cflock> 

    This code is inefficient and can lead to errors if the query takes longer than the timeout on the lock.

  • Creating locks over and over in a template can hurt application performance, and needed locks that are missing can lead to server instability and crashes.

    It is a better practice to set the application variables to the local scope, or better yet, the request scope:

     <cflock scope="application" type="readonly" timeout="10">       <cfset request.dsn=application.dsn>  </cflock>  <cfquery name="AuthenticateUser" datasource=request.dsn>  SELECT UserID       FROM Users       WHERE Email = '#form.email#'            AND Password = '#form.password#'  </cfquery> 

The application scope is a structure. Deleting application variables is as easy as deleting any value held in a structure. You use the StructDelete() function and specify the structure name and key to be deleted:

 <cflock scope="application" type="exclusive" timeout="10">      <cfif NOT IsDefined("application.dsn")>          <cfset tmp=StructDelete(application, "dsn")>      </cfif>  </cflock> 

As we've said, application variables are great for setting variables that stay the same throughout the application. If you want to set variables that are specific to a single user, or to a single session of that specific user, you might consider the use of session variables.



Inside ColdFusion MX
Inside Coldfusion MX
ISBN: 0735713049
EAN: 2147483647
Year: 2005
Pages: 579

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net