User Guide
236 Chapter 12 Using the Application Framework
For example, if you want to assign the results of a query to a Session variable, first get
the query results using a Variables scope variable in unlocked code. Then, assign the
query results to a Session variable inside a locked code section. The following code
illustrates this technique:
<cfquery name="Variables.qUser" datasource="#request.dsn#">
SELECT FirstName, LastName
FROM Users
WHERE UserID = #request.UserID#
</cfquery>
<cflock scope="Session" timeout="2" type="exclusive">
<cfset Session.qUser = Variables.qUser>
</cflock>
Using administrative lock management
You can specify several types of automatic locking and lock checking in ColdFusion
Administrator. Use these options when you are developing your code and if you
must maintain existing, poorly locked code.
Automatic lock checking and locking
The Locking page on the Server tab in the ColdFusion Administrator lets you specify
the following for variables in each of the three shared memory scopes: Session,
Application, and Server.
Selecting the No automatic checking or locking option results in the most efficient
code, but requires you to follow the full rules of locking. You should only select this
option after you finish debugging your program.
Full checking is very useful for debugging your code. You get errors to help indicate
missing locks. You can leave this feature on in production code to protect against
inadvertently unlocked variable accesses, but it adds processor overhead for
checking all accesses to shared variables, and all locking problems cause exceptions.
Type Description
No automatic
checking or locking
ColdFusion does not check for lock use and does not prevent
any invalid access of shared variables.
Full checking ColdFusion generates an exception error when your application
attempts to use any variable in the scope without protecting it
with a lock.
Automatic read
locking
If your application reads a variable without protecting it,
ColdFusion creates a read-only lock for the duration of the read.
As a result, ColdFusion blocks any attempt to write to the
variable (using code within a lock) until the read completes and
the read-only lock is released. If your application writes to any
variable in the scope without protecting it with a lock,
ColdFusion generates an exception error.