MACROMEDIA COLDFUSION MX 61-DEVELOPING COLDFUSION MX Develop Manual page 339

Developing coldfusion mx applications
Table of Contents

Advertisement

Note: You can skip any pair of lock and unlock steps in the preceding list if you do not need to lock a
particular scope. For example, you can omit steps 3 and 4 if you do not need to lock the Server
scope.
Copying shared variables into the Request scope
You can avoid locking some shared-scope variables multiple times during a request by doing the
following:
Copy the shared-scope variables into the Request scope in code with an exclusive lock the
1
Application.cfm page.
Use the Request scope variables on your ColdFusion pages for the duration of the request.
2
Copy the variables back to the shared scope in code with an exclusive lock on the
3
OnRequestEnd.cfm page.
With this technique the "last request wins." For example, if two requests run simultaneously, and
both requests change the values of data that was copied from the shared scope, the data from the
last request to finish is saved in the shared scope, and the data from the previous request is not
saved.
Locking application variables efficiently
The need to lock application variables can reduce server performance, because all requests that use
Application scope variables must wait on a single lock. This issue is a problem even for write-once
read-many variables, because you still must ensure the variable exists, and possibly set the value
before you can read it.
You can minimize this problem by using a technique such as the following to test for the existence
of application variables and set them if they do not exist:
Use an Application scope flag variable to indicate if the variable or variables are initialized. In a
1
read-only lock, check for the existence of the flag, and assign the result to a local variable.
Outside the
2
cflock
If it the local variable indicates that the application variables are not initialized, get an exclusive
3
Application scope lock.
Inside the lock, again test the Application scope flag, to make sure another page has not set the
4
variables between step one and step four.
If the variables are still not set, set them and set the Application scope flag to true.
5
Release the exclusive lock.
6
The following code shows this technique:
<!--- Initilialize local flag to false --->
<cfset app_is_initialized = False>
<!--- Get a readonly lock --->
<cflock scope="application" type="readonly">
<!--- read init flag and store it in local variable --->
<cfset app_is_initialized = IsDefined("APPLICATION.initialized")>
</cflock>
<!--- Check the local flag --->
<cfif not app_is_initialized >
<!--- Not initialized yet, get exclusive lock to write scope --->
<cflock scope="application" type="exclusive">
<!--- Check nonlocal flag since multiple requests could get to the
exclusive lock --->
<cfif not IsDefined("APPLICATION.initialized") >
bock, test the value of the local variable
Locking code with cflock
339

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the COLDFUSION MX 61-DEVELOPING COLDFUSION MX and is the answer not in the manual?

Questions and answers

This manual is also suitable for:

Coldfusion mx

Table of Contents