User Guide

Locking Code with cflock 235
You can set the attribute to any of the following values:
If multiple code sections share a lock, the following rules apply:
When code in a
cflock tag block with the type Exclusive is running, code in
blocks with the same lock are not allowed run. They wait until the code with the
Exclusive lock completes.
When code in a cflock tag block with the type read-only is running, code in other
blocks with the same lock and the read-only type can run, but any blocks with the
exclusive type cannot run and wait until all code with the read-only lock
completes.
Controlling locking access to files and CFX tags with the name attribute
The cflock name attribute provides a second mechanism for identifying locks. Use
this attribute when you use locks to protect code that manges file access or calls
non-thread-safe CFX code.
When you use the
name attribute, specify the same name for each section of code that
accesses a specific file or a specific CFX tag.
Controlling lock timeouts
You must include a timeout attribute in your cflock tag. It specifies the maximum
time, in seconds, to wait to obtain the lock if it is not available. By default, if the lock
does not become available within the timeout period, ColdFusion generates an
exception error, which you can handle using
cftry and cfcatch.
If you set the
cflock throwOntTmeout attribute to No, processing continues after the
timeout at the line after the
</cflock> end tag.
Under normal circumstances it should not take more than a few seconds to obtain a
lock. Very large timeouts can block request threads for long periods of time and
radically decrease throughput. Always use the smallest timeout value that does not
result in significant numbers of timeouts.
To prevent unnecessary timeouts, lock the minimum amount of code possible.
Whenever possible lock only code that sets or reads variables, not business logic or
database queries. One useful technique is to perform a time-consuming activity
outside of a
cflock tag and assign the results to a Variables scope variable, then
assign the shared scope variable to the Variables scope variables value inside a
cflock block.
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 and
application share a single lock.