Using The Cflock Tag With Write-Once Variables; Using The Cflock Tag - MACROMEDIA COLDFUSION MX 61-DEVELOPING COLDFUSION MX Develop Manual

Developing coldfusion mx applications
Table of Contents

Advertisement

Order 2 exits the lock tag, which frees the Application scope lock. ColdFusion can process
11
another order.
The resulting Application.totalTickesSold value is now correct.
Ensuring consistency of multiple variables
Often an application sets multiple shared scope variables at one time, such as a number of values
submitted by a user on a form. If the user submits the form, clicks the back button, and then
resubmits the form with different data, the application might end up with a mixture of data from
the two submissions, in much the same manner as shown in the previous section.
For example, an application might store information about order items in a Session scope
shopping cart. If the user submits an item selection page with data specifying sage green size 36
shorts, and then resubmits the item specifying sea blue size 34 shorts, the application might end
up with a mixture of information from the two orders, such as sage green size 34 shorts.
By putting the code that sets all of the related session variables in a single
that all the variables get set together. In other words, setting all of the variables becomes an
atomic, or single, operation. It is similar to a database transaction, where everything in the
transaction happens, or nothing happens. In this example, the order details for the first order all
get set, and then they are replaced with the details from the second order.
For more examples of using locking in applications, see

Using the cflock tag with write-once variables

You do not need to use
the Session, Application, or Server scope if it is set in only one place in the application, and is only
read (or called, for a UDF) everywhere else. Such data is called write-once. If you set an
Application or Session scope variable in Application.cfm and never set it on any other pages, you
must lock the code that sets the variable, but do not have to lock code on other pages that reads
the variable's value.
However, although leaving code that uses write-once data unlocked can improve application
performance, it also has risks. You must make sure that the variables are truly written only once.
For example, you must make sure that the variable is not rewritten if the user refreshes the
browser or clicks a back button. Also, it can be difficult to ensure that you, or future developers,
do not later set the variable in more than one place in the application.

Using the cflock tag

The
tag ensures that concurrently executing requests do not run the same section of code
cflock
simultaneously and thus manipulate shared data structures, files, or CFX tags inconsistently. It is
important to remember that
variables themselves.
You protect access to code by surrounding it in a
<cflock scope="Application" timeout="10" type="Exclusive">
<cfif not IsDefined("Application.number")>
<cfset Application.number = 1>
</cfif>
</cflock>
when you read a variable or call a user-defined function name in
cflock
protects code sections that access or set data, not the
cflock
"Examples of cflock" on page
tag; for example:
cflock
Locking code with cflock
tag, you ensure
cflock
340.
335

Advertisement

Table of Contents
loading

This manual is also suitable for:

Coldfusion mx

Table of Contents