User Guide

Examples of cflock 241
Example of synchronizing access to a file system
The following example shows how to use cflock to synchronize access to a file
system. The
cflock tag protects a cffile tag from attempting to append data to a
file already open for writing by the same tag executing on another request.
Note that if an append operation takes more that 30 seconds, a request waiting to
obtain an exclusive lock to this code might time out. Also, note the use of a dynamic
value for the
name attribute so that a different locks controls access to each file. As a
result, locking access to one file does not delay access to any other file.
<cflock name=#filename# timeout=30 type="Exclusive">
<cffile action="Append"
file=#fileName#
output=#textToAppend#>
</cflock>
Example of protecting ColdFusion Extensions
The following example illustrates how you can build a custom tag wrapper around a
CFX tag that is not thread-safe. The wrapper forwards attributes to the
non-thread-safe CFX tag that is used inside a cflock tag.
<cfparam name="Attributes.AttributeOne" default="">
<cfparam name="Attributes.AttributeTwo" default="">
<cfparam name="Attributes.AttributeThree" default="">
<cflock timeout=5
type="Exclusive"
name="cfx_not_thread_safe">
<cfx_not_thread_safe attributeone=#attributes.attributeone#
attributetwo=#attributes.attributetwo#
attributethree=#attributes.attributethree#>
</cflock>