cflock

Description

Ensures the integrity of shared data. Instantiates the following kinds of locks:

Categories

Related

History

ColdFusion 8: Added Request value to scope attribute.

Syntax

<cflock  
    timeout = "time-out in seconds" 
    name = "lock name" 
    scope = "Application|Server|Session|Request" 
    throwOnTimeout = "yes|no" 
    type = "readOnly|exclusive">  
    <!--- CFML to be synchronized. --->  
</cflock>
Note: You can specify this tag’s attributes in an attributeCollection attribute whose value is a structure. Specify the structure name in the attributeCollection attribute and use the tag’s attribute names as structure keys.

Attributes

AttributeDescriptionRequiredDefault
nameLocks name. Mutually exclusive with the scope attribute. Only one request can execute the code within a cflocktag with a given name at a time. Cannot be an empty string. Permits synchronizing access to resources from different parts of an application. Lock names are global to a ColdFusion server. They are shared among applications and user sessions, but not clustered servers.Optional
scopeLocks scope. Mutually exclusive with the name attribute. Only one request in the specified scope can execute the code within this tag (or within any other cflock tag with the same lock scope) at a time. Application Request Server SessionOptional
throwOnTimeoutHow time-out conditions are handled: yes: exception is generated for the time-out. no: execution continues past this tag.Optionalyes
timeoutMaximum length of time, in seconds, to wait to obtain a lock. If lock is obtained, tag execution continues. Otherwise, behavior depends on throwOnTimeout attribute value. If you set timout="0", the timeout is determined by the "Timeout Requests after x" setting in the ColdFusion Administrator Settings page, if that setting is enabled. However, if the setting is not enabled, and you set timeout="0", ColdFusion can wait indefinitely to obtain the lock.Required
typereadOnly: lets more than one request read shared data. exclusive: lets one request read or write shared data.Optionalexclusive

Usage

ColdFusion is a multi-threaded server; it can process multiple page requests at a time. Use the cflock tag for these purposes:
To ensure that modifications to shared data and objects made in concurrently executing requests occur sequentially.
Around file manipulation constructs, to ensure that file updates do not fail because files are open for writing by other applications or tags.
Around CFX invocations, to ensure that ColdFusion can safely invoke CFXs that are not implemented in a thread-safe manner. (This applies only to CFXs developed in C++.)
To work safely with ColdFusion, a C++ CFX that maintains and manipulates shared (global) data structures must be made thread-safe; however, this requires advanced knowledge. You can use a CFML custom tag wrapper around a CFX to make its invocation thread-safe.
When you display, set, or update variables in a shared scope, use the scope attribute to identify the scope as Server, Application or Session.

Example

<!---  
This example shows how cflock can guarantee consistency of data updates to variables in the 
    Application, Server, and Session scopes. ---> 
 
<!--- Copy the following code into an Application.cfm file in the  
    application root directory. ---> 
<!----------------   Beginning of Application.cfm code   ---------------> 
<!--- cfapplication defines scoping for a ColdFusion application and enables or disables 
    storing of application and session variables. Put this tag in a special file called 
    Application.cfm. It is run before any other ColdFusion page in its directory. ---> 
 
<!--- Enable session management for this application. ---> 
<cfapplication name = "ETurtle"  
    sessionTimeout = #CreateTimeSpan(0,0, 0, 60)#  
    sessionManagement = "yes"> 
 
<!--- Initialize session and application variables used by E-Turtleneck. Use session scope 
    for the session variables. --->  
<cflock scope = "Session"  
    timeout = "30" type = "Exclusive"> 
    <cfif NOT IsDefined("session.size")> 
        <cfset session.size = ""> 
    </cfif> 
    <cfif NOT IsDefined("session.color")> 
        <cfset session.color = ""> 
    </cfif> 
</cflock> 
 
<!--- Use an application lock for the application-wide variable that keeps track of the 
    number of turtlenecks sold. For a more efficient, but more complex, way of handling 
    Application scope locking, see the "Developing ColdFusion Applications"---> 
<cflock scope = "Application" timeout = "30" type = "Exclusive"> 
    <cfif NOT IsDefined("application.number")> 
        <cfset application.number = 0> 
    </cfif> 
</cflock> 
 
<!----------------------- End of Application.cfm -----------------------> 
 
<h3>cflock Example</h3> 
 
<cfif IsDefined("form.submit")> 
<!--- The form has been submitted; process the request. ---> 
    <cfoutput> 
        Thanks for shopping E-Turtleneck. You chose size <b>#form.size#</b>,  
        color <b>#form.color#</b>.<br><br> 
    </cfoutput> 
 
<!--- Lock the code that assigns values to session variables. ---->  
    <cflock scope = "Session" timeout = "30" type = "Exclusive"> 
        <cfparam name = session.size Default = #form.size#> 
        <cfparam name = session.color Default = #form.color#> 
    </cflock> 
 
<!---- Lock the code that updates the Application scope number of turtlenecks sold. --->  
    <cflock scope = "Application" timeout = "30" type = "Exclusive"> 
        <cfset application.number = application.number + 1> 
    <cfoutput> 
        E-Turtleneck has now sold #application.number# turtlenecks! 
    </cfoutput> 
    </cflock> 
 
<cfelse> 
<!--- Show the form only if it has not been submitted. ---> 
    <cflock scope = "Application" timeout = "30" type = "Readonly"> 
        <cfoutput> 
            E-Turtleneck has sold #application.number# turtlenecks to date. 
        </cfoutput> 
    </cflock> 
 
    <form method="post" action="cflocktest.cfm"> 
        Congratulations! You selected the most comfortable turtleneck in the world. 
        Please select color and size.</p>  
        <table cellspacing = "2" cellpadding = "2" border = "0"> 
            <tr> 
                <td>Select a color.</td> 
                <td><select type = "Text" name = "color"> 
                        <option>red 
                        <option>white 
                        <option>blue 
                        <option>turquoise 
                        <option>black 
                        <option>forest green 
                    </select> 
                </td> 
            </tr> 
            <tr> 
                <td>Select a size.</td> 
                <td><select type = "Text" name = "size" > 
                        <option>XXsmall 
                        <option>Xsmall 
                        <option>small 
                        <option>medium 
                        <option>large 
                        <option>Xlarge 
                    </select> 
                </td> 
            </tr> 
            <tr> 
                <td>Press Submit when you are finished making your selection.</td> 
                <td><input type = "Submit" name = "submit" value = "Submit"> </td> 
            </tr> 
        </table> 
    </form> 
</cfif>