Provides two types of locks to ensure the integrity of shared data:
An exclusive lock single-threads access to the CFML constructs in its body. Single-threaded access implies that the body of the tag can be executed by at most one request at a time. A request executing inside a cflock
tag has an "exclusive lock" on the tag. No other requests can start executing inside the tag while a request has an exclusive lock. ColdFusion issues exclusive locks on a first-come, first-served basis.
A read-only lock allows multiple requests to access the CFML constructs inside its body concurrently. Therefore, read-only locks should be used only when the shared data is read only and not modified. If another request already has an exclusive lock on the shared data, the request waits for the exclusive lock to be released.
Web application framework tags
<cflock timeout = "timeout in seconds " scope = "Application" or "Server" or "Session" name = "lockname" throwOnTimeout = "Yes" or "No" type = "readOnly/Exclusive "> <!--- CFML to be synchronized ---> </cflock>
cfapplication,
cfassociate,
cfauthenticate,
cfmodule
Note Limit the scope of code that updates shared data. Exclusive locks are required to ensure the integrity of these updates, but they have a significant impact on performance. Read-only locks are faster. If you have a performance-sensitive application, substitute read-only locks for exclusive locks where possible; for example, when reading shared data. |
ColdFusion Server is a multi-threaded web application server that can process multiple page requests at a time. Use cflock
to guarantee that concurrently executing requests do not manipulate shared data structures, files, or CFXs inconsistently.
cflock
around CFML constructs that modify shared data ensures that the modifications occur sequentially, not concurrently.
cflock
around file manipulation constructs can guarantee that file updates do not fail due to files being open for writing by other applications or ColdFusion tags.cflock
around CFX invocations can guarantee that CFXs that are not implemented in a thread-safe manner can be safely invoked by ColdFusion. This usually only applies to CFXs developed in C++ using the CFAPI. A C++ CFX that maintains and manipulates shared (global) data structures must be made thread-safe to safely work with ColdFusion. However, writing thread-safe C++ CFXs requires advanced knowledge. A CFML custom tag wrapper can be used around the CFX to make its invocation thread-safe.
When you display, set, or update variables in a shared scope, use the scope
attribute to identify the scope as Server, Application or Session.
In the ColdFusion Administrator, the Locking page, under the Server section, lets you set characteristics of the locking schema according to scope. The following table shows which features are available for Server, Application, and Session scope.
Feature |
Availability: |
Server |
Application |
Session |
---|---|---|---|---|
No automatic checking or locking |
Yes |
Yes |
Yes |
|
Full checking |
Yes |
Yes |
Yes |
|
Automatic read locking |
Yes |
Yes |
Yes |
|
Single Threaded Sessions |
|
|
Yes |
Each feature that you select has tradeoffs:
For more information, see Advanced ColdFusion Administration.
If you create a lock with the name
attribute, not with the scope
attribute, and enable full lock checking in the ColdFusion Administrator, ColdFusion returns an error.
The cflock
tag uses kernel level synchronization objects that are released automatically upon timeout and/or abnormal termination of the thread that owns them. Therefore, ColdFusion never deadlocks for an infinite period of time while processing a cflock
tag. However, very large timeouts can block request threads for long periods of time and radically decrease throughput. Always use the minimum timeout value allowed.
Another cause of blocked request threads is inconsistent nesting of cflock
tags and inconsistent naming of locks. If you nest locks, you and everyone accessing the locked variables must consistently nest cflock
tags in the same order. If everyone accessing locked variables does not adhere to these conventions, a deadlock can occur. A deadlock is a state in which no request can execute the locked section of the page. All requests to the protected section of the page are blocked until there is a timeout. The following tables show two scenarios that cause deadlocks.
The following deadlock scenario could take place if you tried to nest a write lock after a read lock:
The following code shows this scenario:
<cflock timeout = "60" scope = "SESSION" type = "readOnly">
............... <cflock timeout = "60" scope = "SESSION" type = "Exclusive"> ......... </cflock> </cflock>
Once a deadlock occurs, neither user can break the deadlock, because the execution of their requests is blocked until the deadlock can be resolved by a lock timeout.
To avoid a deadlock, you and all who nest locks should do so in a well-specified order and name the locks consistently. If you must lock access to the server, application, and session scopes, you must do so in the following order.
cflock
tag, indicate the scope by specifying "SESSION" as the value of the scope
attribute.
cflock
tag, indicate scope by specifying "Application" as the value of the scope
attribute.cflock
tag, indicate the scope by specifying "server" as the value of the scope
attribute.
Note You can take out any pair of lock/unlock steps if you do not have to lock a scope. For example, you can take out Steps 3 and 4 if you do not have to lock the server scope. Similar rules apply for named locks. |
For more information on cflock
, see Developing ColdFusion Applications.
<!------------------------------------------------------------- This example shows how cflock can be used to guarantee the consistency of data updates to variables in the Application, Server, and Session scopes. You should copy the following code into an Application.cfm file in the snippets directory. ---------------------------------------------------------------> <html> <head> <title>Define Session and Application Variables</title> </head> <BASEFONT face = "Arial, Helvetica" size = 2> <body bgcolor = "#FFFFD5"> <H3>cfapplication Example</H3> <P>cfapplication defines scoping for a ColdFusion application and enables or disables the storing of application and/or session variables. This tag is placed in a special file called Application.cfm that is run before any other CF template in a directory where the Application.cfm file appears.</P> <cfapplication name = "ETurtle" sessionTimeout = #CreateTimeSpan(0,0, 0, 60)# sessionManagement = "Yes"> <!--------------------------------------------------------------------- Initialize the session and application variables that will be used by E-Turtleneck. Use the session scope for the session variables. ----------------------------------------------------------------------> <cflock scope = "Session" timeout = "30" type = "Exclusive"> <cfif NOT IsDefined("session.size")> <cfset session.size = ""> </cfif> <cfif NOT IsDefined("session.color")> <cfset session.color = ""> </cfif> </cflock> <!--------------------------------------------------------------------- Use the application lock for the application variable. This variable keeps track of the total number of turtlenecks sold. The application lock should have the same name as specified in the cfapplication tag. ----------------------------------------------------------------------> <cflock scope = "Application" Timeout = "30" type = "Exclusive"> <cfif NOT IsDefined("application.number")> <cfset application.number = 1> </cfif> </cflock> <cflock scope = "Application" timeout = "30" type = "readOnly"> <cfoutput> E-Turtleneck is proud to say that we have sold #application.number# turtlenecks to date. </cfoutput> </cflock> <!--------------------------------------------------------------------- End of Application.cfm ----------------------------------------------------------------------> <head> <title> cflock Example </title> </head> <BASEFONT face = "Arial, Helvetica" size = 2> <body bgcolor = "#FFFFD5"> <H3>cflock Example</H3> <cfif IsDefined("form.submit")> <cfoutput> Thank you for shopping E-Turtleneck. Today you have chosen a turtleneck in size <b>#form.size#</b> and in the color <b>#form.color#</b>. </cfoutput> <!----------------------------------------------------------------- Lock session variables to assign form values to them. -------------------------------------------------------------------> <cflock scope = "Session" timeout = "30" type = "Exclusive"> <cfparam name = session.size Default = #form.size#> <cfparam name = session.color Default = #form.color#> </cflock> <!------------------------------------------------------------------ Lock application variable application.number to find the total number of turtlenecks sold. -------------------------------------------------------------------> <cflock scope = "Application" timeout = "30" type = "Exclusive"> <cfset application.number = application.number + 1> </cflock> <cfelse><!--- Show the form only if it has not been submitted. ---> <form action = "cflock.cfm" method = "POST"> <P>Congratulations! You have just selected the longest wearing, most comfortable turtleneck in the world. Please indicate the color and size that you wish to buy.</P> <table cellspacing = "2" cellpadding = "2" border = "0"> <tr> <td>Select a color.</td> <td><select type = "Text" name = "color"> <option>red <option>white <option>blue <option>turquoise <option>black <option>forest green </select> </td> </tr> <tr> <td>Select a size.</td> <td><select type = "Text" name = "size" > <option>XXsmall <option>Xsmall <option>small <option>medium <option>large <option>Xlarge </select> </td> </tr> <tr> <td>Press Submit when you are finished making your selection.</td> <td><input type = "Submit" name = "submit" value = "Submit"> </td> </tr> </table> </form> </cfif> </html>