Using Application Variables to Preserve Data Throughout an Application

 < Day Day Up > 



The third item in ColdFusion's client-state management toolbox is the application variable. While they're not used as client and session variables, application variables still serve a vital purpose. They enable you to define variables that persist as long as the application is running, or usually from the time the application is first accessed by any user until the server is shut down or restarted.

The unique thing about application variables is that unlike session and client variables, they aren't associated with any particular user. This means that their values are the same for every user of your application.

Note 

Unlike session and client variables, application variables are enabled by default any time you use an  Application.cfm file.

For example, you could use an application variable to create a simple page counter that would show how many times a page has been loaded since the application has been running:

<cfparam name="application.counter" default = "0"> <cfset application.counter = application.counter + 1>     <cfoutput>This page has been viewed #application.counter# times since  the server was last restarted</cfoutput>

Each time any user loads a page containing that snippet, the global value of application.counter is increased by 1 and the current count is displayed to the user. Again, note that application variables are not relative to any user. For example, the 301st user would see a count of "301," not "1" as he would with a session or client variable. Application variables are good places to store data that will be accessed by many pages in your application but does not need to be user-specific.

start sidebar
Using <cflock> to Ensure Data Integrity on Busy Sites

When you use session or application variables, there's a danger that two or more users may access a page at the same time. This would cause ColdFusion to simultaneously read or write two chunks of data to one memory space on your server, which could result in inaccurate data being recorded or displayed to users.

The solution is to use the <cflock> tag wherever session or application variables are used. Using <cflock> ensures that ColdFusion places simultaneous read or write requests in a queue and processes them one by one. When a section of code enclosed in <cflock> tags is encountered, ColdFusion gives exclusive priority to that session until the code is complete.

For example, to ensure the integrity of an application variable, you use <cflock> like this:

<cflock scope="application" type="exclusive">      <cfset application.counter = application.counter + 1>      <cfoutput>This page has been viewed #application.counter# times  since the server was last restarted</cfoutput> 

This code would prevent an incorrect value of application.counter from being displayed if a second user somewhere on the system happened to access this page in the split second between the time the first user's application.counter was incremented and the time it was displayed in his or her browser.

The scope attribute used with <cflock> specifies whether the variable you're locking should be locked at the application, session, or server level. In most cases you'll use the application scope for application variables, or the session scope when locking session variables. The third scope, server, is rarely used in typical ColdFusion applications since it's designed to work with variables that apply server-wide, i.e., for all applications running under an installation of ColdFusion MX.

The type attribute tells <cflock> whether the variable should be locked for exclusive access, as in the example above, or whether the variable will only be read rather than written to, in which case you would use the type "read-only."

end sidebar



 < Day Day Up > 



Macromedia Studio MX Bible
Macromedia Studio MX Bible
ISBN: 0764525239
EAN: 2147483647
Year: 2003
Pages: 491

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