User Guide
234 Chapter 12 Using the Application Framework
The cflock tag enables you to ensure that concurrently executing requests do not
access the same section of code simultaneously and thus manipulate shared data
structures, files, or CFXs inconsistently. It is important to remember that
cflock
protects code sections not variables.
Lock types
The cflock tag offers two modes of locking, specified by the type attribute:
• Exclusive locks (the default lock type) allow only one request to process the
locked code. No other requests are allowed to start running code inside the tag
while a request has an exclusive lock. ColdFusion issues exclusive locks on a
first-come, first-serve basis.
Enclose all code that creates or modifies Session, Application, or Server variables
in exclusive
cflock tags.
• Read-only locks allow multiple requests to execute concurrently, provided that
no exclusive locks are executing. ColdFusion allows you to set variables inside
read-only lock tag blocks. However, if you do set a shared variable inside a
read-only lock tag, you lose the advantages of locking. As a result, you must be
careful not to set any Session, Application, or Server variables inside a read-only
cflock tag body.
Enclose code that reads or tests Session, Application, or Server variables in
exclusive
cflock tags. You specify a read-only lock by setting the
type="readOnly" attribute in the cflock tag.
Lock scopes and names
The cflock tag prevents simultaneous access to a sections of code, not variables. If
you have two sections of code that access the same variable, they too must be
synchronized to prevent them form running simultaneously. You do this by
identifying the locks with either
scope or name attributes.
Note
ColdFusion does not require you to identify Exclusive locks. If you omit the
identifier, the lock is anonymous and you cannot synchronize the code in the
cflock
tag block with any other code. It is acceptable to use an anonymous lock only when
the resource you are protecting is used nowhere else in your code. You must always
identify read-only locks.
Controlling access to data with the scope attribute
When the code that you are locking accesses Session, Application, or Server
variables, synchronize access by using the
cflock scope attribute.