Alex Rivera | Logout

When should I use Scope Locking (Application, Server, etc...) vs named locking in ColdFusion?

Asked 2008-11-17T20:07:19.683
11

When is it appropriate to use <cflock scope="application"> or it's ilk as opposed to <cflock name="foo">?

Specifically, I'm interested in using CFLock to protect shared objects in the application, session, or server scopes, but I'm also interested in finding out about different uses of locking in ColdFusion.

Edit
Report

1 Answer

0

This is an example from the ColdFusion 8 documentation that uses a page variable to create a "local flag" that can be read without locking to see if the application variables have been initialized.

What this does address is the fact that we need to conditionalize the exclusive lock because running it every time a page is loaded may create bottlenecks due to the lock taking up more processing time.

I don't know if better techniques have appeared since this has, but I figure I'll post it here anyway. The ColdFusion docs often don't provide good code, so I'm interested to see if anyone can see how this one can be improved.

  • I might put the local flag in the request scope so that it is available even in custom tags, etc. However, it's really only needed in app.cfm, so maybe that's unnecessary.
  • I also would remove isDefined() in favor of structKeyExists() so that it doesn't have to cycle through all the scopes.
  • I would also use bracket notation to set variables, so that capitalization is preserved (e.g. application['myDsn'] = "orders"). It also makes it easier to spot variable writes, which are a bit more important than variable reads. (This one's just my preference)

Source: http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=sharedVars_18.html

<!--- 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
answered 2013-07-17T13:29:17.120

Your Answer