User Guide

Table Of Contents
364 Chapter 15: Using Persistent Data and Locking
</cfif>
</cflock>
Although ColdFusion does not prevent you from setting shared variables inside read-only lock
tag, doing so loses 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.
Note: You cannot upgrade or downgrade a lock from one type to another. In other words, do not nest
an exclusive lock in a read-only lock of the same name or scope; the exclusive lock will always time
out. Also, do not nest a read-only lock inside an exclusive lock with the same name or scope; doing so
has no effect.
Lock scopes and names
The
cflock tag prevents simultaneous access to sections of code, not to variables. If you have two
sections of code that access the same variable, they must be synchronized to prevent them from
running simultaneously. You do this by identifying the locks with the same
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.
Anonymous locks do not cause errors when they protect a resource that is used in a single code
block, but they are bad programming practice. 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.
You can set the attribute to any of the following values:
If multiple code sections share a lock, the following rules apply:
When code is running in a cflock tag block with the type attribute set to Exclusive, code in
cflock tag blocks with the same scope attribute is not allowed to run. They wait until the
code with the exclusive lock completes.
When code in a cflock tag block with the type readOnly is running, code in other cflock
tag blocks with the same
scope attribute and the readOnly type attribute can run, but any
blocks with the same
scope attribute and an Exclusive type cannot run and must wait until
all code with the read-only lock completes. However, if a read-only lock is active and code with
an exclusive lock with the same scope or name is waiting to execute, read-only requests using
the same scope or name that are made after the exclusive request is queued must wait until
code with the exclusive lock executes and completes.
Scope Meaning
Server All code sections with this attribute on the server share a single lock.
Application All code sections with this attribute in the same application share a single lock.
Session All code sections with this attribute that run in the same session of an application
share a single lock.