Whatever message this page gives is out now! Go check it out!
<cfset Application.totalTicketsSold = Application.totalTicketsSold + ticketOrder>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">
<cfif not IsDefined("Application.number")>
<cfset Application.number = 1>
</cfif>
</cflock><cflock scope="Application" timeout="10" type="readOnly">
<cfif IsDefined("Application.dailyMessage")>
<cfoutput>#Application.dailyMessage#<br></cfoutput>
</cfif>
</cflock>Scope | Meaning |
Server | All code sections with this attribute on the server share a single lock. |
Application | All code sections with this attribute in the same application share a single lock. |
Session | All code sections with this attribute that run in the same session of an application share a single lock. |
Request | All code sections with this attribute that run in the same request share a single lock. You use this scope only if your application uses the cfthread tag to create multiple threads in a single request. Locking the Request scope also locks access to Variables scope data. For more information on locking the Request scope, see Locking thread data and resource access. |
<cfquery name="Variables.qUser" datasource="#request.dsn#">
SELECT FirstName, LastName
FROM Users
WHERE UserID = #request.UserID#
</cfquery>
<cflock scope="Session" timeout="5" type="exclusive">
<cfset Session.qUser = Variables.qUser>
</cflock>User 1 | User 2 |
Locks the Session scope. | Locks the Application scope. |
Tries to lock the Application scope, but the Application scope is already locked by User 2. | Tries to lock the Session scope, but the Session scope is already locked by User 1. |
<!--- Initialize 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") >
<!--- Do initializations --->
<cfset APPLICATION.varible1 = someValue >
...
<!--- Set the Application scope initialization flag --->
<cfset APPLICATION.initialized = "yes">
</cfif>
</cflock>
</cfif>