BackUp LevelNext

Using CFLOCK for Exclusive Locking

The CFLOCK tag single-threads access to the CFML constructs in its body, so that the body of the tag can be executed by at most one request at a time. A request executing inside a CFLOCK tag has an "exclusive lock" on the tag. No other requests are allowed to start executing inside the tag while a request has an exclusive lock. ColdFusion issues exclusive locks on a first-come first-serve basis.

Using CFLOCK around CFML constructs that modify shared data ensures that the modifications occur one after the other and not all at the same time. As a general rule, you should use the CFLOCK tag to perform updates to variables in the Application, Server, and Session scopes in the Application.cfm file.

Example

<!--- This example shows how CFLOCK can be used to
guarantee the consistency of data updates to variables
in the Application, Server, and Session scopes. The 
following code might be part of Application.cfm. --->

<HTML>
<HEAD>CFLOCK Example</HEAD>

<BODY>
<H3>CFLOCK Example</H1>

<CFLOCK NAME="ApplicationData" TIMEOUT=30>
    <CFIF NOT IsDefined("Application.IsApplicationDataInitialized")>
        <CFSET Application.IsApplicationDataInitialized=TRUE>
        <CFSET Application.ImportantValue = 5>
    </CFIF>
</CFLOCK>
<CFOUTPUT>
    Important value is #Application.ImportantValue#
</CFOUTPUT>

</BODY>
</HTML>

See the CFML Language Reference for more information on using CFLOCK.


BackUp LevelNext

allaire

AllaireDoc@allaire.com
Copyright © 1998, Allaire Corporation. All rights reserved.