Adobe 38043740 - ColdFusion Standard - Mac Development Manual page 324

Developing applications
Hide thumbs Also See for 38043740 - ColdFusion Standard - Mac:
Table of Contents

Advertisement

DEVELOPING COLDFUSION 9 APPLICATIONS
Developing CFML Applications
Suppose that ColdFusion processes two ticket orders at approximately the same time, and that the value of
Application.totalTicketsSold is initially 160. The following sequence might happen:
Order 1 reads the total tickets sold as 160.
1
2
Order 2 reads the total tickets sold as 160.
Order 1 adds an order of 5 tickets to 160 to get 165.
3
Order 2 adds an order of 3 tickets to 160 to get 163.
4
5
Order 1 saves the value 165 to Application.totalTicketsSold
Order 2 saves the value 163 to Application.totalTicketsSold
6
The application now has an inaccurate count of the tickets sold, and is in danger of selling more tickets than the
auditorium can hold.
To prevent this from happening, lock the code that increments the counter, as follows:
<cflock scope="Application" timeout="10" type="Exclusive">
<cfset Application.totalTicketsSold = Application.totalTicketsSold + ticketOrder>
</cflock>
The
tag ensures that while ColdFusion performs the processing in the tag body, no other threads can access
cflock
the Application scope. As a result, the second transaction is not processed until the first one completes. The processing
sequence looks something like the following:
1
Order 1 reaches the lock tag, which gets an Application scope lock.
Order 1 reads the total tickets sold as 160.
2
Order 2 reaches the lock tag. Because there is an active Application scope lock, ColdFusion waits for the lock to free.
3
4
Order 1 adds an order of 5 tickets to 160 to get 165.
5
Order 1 saves the value 165 to Application.totalTicketsSold.
Order 1 exits the lock tag. The Application scope lock is now free.
6
7
Order 2 gets the Application scope lock and can begin processing.
8
Order 2 reads the total tickets sold as 165.
Order 2 adds an order of 3 tickets to 165 to get 168.
9
Order 2 saves the value 168 to Application.totalTicketsSold.
10
11
Order 2 exits the lock tag, which frees the Application scope lock. ColdFusion can process 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 many 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 can 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 stores 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 can end up with a mixture of information from the two orders, such as sage green size
34 shorts.
Last updated 1/20/2012
319

Advertisement

Table of Contents
loading

This manual is also suitable for:

Coldfusion 9

Table of Contents