User Guide

Locking Code with cflock 233
Locking Code with cflock
The cflock tag controls simultaneous access to ColdFusion code. The cflock tag
enables you to:
Protect sections of code that access and manipulate shared data such as Session,
Application, and Server variables.
Ensure that file updates do not fail because files are open for writing by other
applications or ColdFusion tags.
Ensure that applications do not try to simultaneously access ColdFusion
extension tags written using the CFX API that are not thread-safe. This is only
necessary for CFX tags that are written in C++ and use shared (global) data
structures without protecting them from simultaneous access (are not
thread-safe).
Ensure that applications do not try to simultaneously access databases that are
not thread-safe. This is not necessary for most database systems.
Failure to use
cflock in these circumstances can result in data corruption and can
result in hanging the ColdFusion Server. Symptoms of this corruption include the
following:
Unexpected error messages, particularly Unknown Exception errors
cfserver process crashing, or stopping and restarting
Unexpected values in shared variables
Excessive growth in memory used by the cfserver process
Operating system instability
Using cflock
You protect access to code by surrounding it in a cflock tag; for example:
<cflock scope="Application"
timeout="10"
type="Exclusive">
<cfif not isdefined("application.number")>
<cfset Application.number = 1>
</cfif>
</cflock>
How cflock works
ColdFusion Server is a multithreaded Web application server that can process
multiple page requests at a time. As a result, the server can attempt to access the
same information simultaneously as the result of two or more requests. While it is
safe to read data simultaneously, attempting to write data simultaneously or read
and write it at the same time can result in corrupted memory and can cause the
process to crash.