User Guide

Table Of Contents
368 Chapter 15: Using Persistent Data and Locking
exclusive lock --->
<cfif not IsDefined("APPLICATION.initialized") >
<!--- Do initializations --->
<cfset APPLICATION.varible1 = someValue >
...
<!--- Set the Application scope initialization flag --->
<cfset APPLICATION.initialized = "yes">
</cfif>
</cflock>
</cfif>
Examples of cflock
The following examples show how to use cflock blocks in a variety of situations.
Example with application, server, and session variables
This example shows how you can use
cflock to guarantee the consistency of data updates to
variables in the Application, Server, and Session scopes.
This example does not handle exceptions that arise if a lock times out. As a result, users see the
default exception error page on lock time-outs.
The following sample code might be part of the Application.cfm file:
<cfapplication name="ETurtle"
sessiontimeout=#createtimespan(0,1,30,0)#
sessionmanagement="yes">
<!--- Initialize the Session and Application
variables that will be used by E-Turtleneck. Use
the Session lock scope for the session variables. --->
<cflock scope="Session"
timeout="10" type ="Exclusive">
<cfif not IsDefined("session.size")>
<cfset session.size = "">
</cfif>
<cfif not IsDefined("session.color")>
<cfset session.color = "">
</cfif>
</cflock>
<!--- Use the Application scope lock for the Application.number variable.
This variable keeps track of the total number of turtlenecks sold.
The following code implements the scheme shown in the Locking Application
variables effectively section --->
<cfset app_is_initialized = "no">
<cflock scope="Application" type="readonly">
<cfset app_is_initialized = IsDefined("Application.initialized")>
</cflock>
<cfif not app_is_initialized >
<cflock scope="application" timeout="10" type="exclusive">
<cfif not IsDefined("Application.initialized") >