In Model-Glue, Fusebox, and Mach-II, you'll see a bit of code surrounding the instantiation of the "core" framework CFC that looks a bit like this:


<code>

<cfif not frameworkIsLoaded>
  <cflock name="frameworkLoading">
    <cfif not frameworkIsLoaded>
      <!--- load framework --->
    </cfif>
  </cflock>
</cfif>

</code>

At first glance, the &lt;cfif> statements seem redundant.  However, they form what's known as a "double-checked lock," and they're quite important.

Let's pretend that Jim and Bob are both about to go to http://www.firemoss.com, a site running Model-Glue.  Let's also pretend that nobody's been there for a while (hopefully not true), and the application has timed out.   

This is what happens with double-checked locking:

Jim:  "Send me index.cfm."

MG (to Jim):  "One moment, my first &lt;cfif> says the framework isn't loaded.  I'll start loading it now, and lock the piece of code that does the load."

Bob:  "Send me index.cfm."

MG (to Bob):  "Ok, my first &lt;cfif> says the framework isn't loaded, but there's a &lt;cflock> tag here that says you need to wait a moment while I load the framework for Jim."

MG (to Jim):  "The loading is done, I'll start processing your page now."

MG (to Bob):  "Ok, the lock is free, you can enter the loading code now.  However, there's a second <cfif> that tells me that the framework was loaded while you were waiting, so I'm going to skip loading the framework again.  Here's your page."

Without the second &lt;cfif that forms the double-checked locking, the conversation could take a sour tone:

Jim:  "Send me index.cfm."

MG (to Jim):  "One moment, my first &lt;cfif> says the framework isn't loaded.  I'll start loading it now, and lock the piece of code that does the load."

Bob:  "Send me index.cfm."

MG (to Bob):  "Ok, my first &lt;cfif> says the framework isn't loaded, but there's a &lt;cflock> tag here that says you need to wait a moment while I load."

MG (to Jim):  "The loading is done, I'll start processing your page now."

MG (to Bob):  "Ok, the lock is free, you can enter the loading code now.  <strong>I'm going to load the framework, again, even though I just did so for Jim, and his request may still be running.</strong>"

MG (to Jim):  "<font color="red">Oops!</font>  I was in the middle of running your request, but I reloaded myself because Bob's request asked me to, and the controller on which I was about to call a listener function just ceased to exist in the application scope, so, well, here's an error message."

Conclusion 

In multi-threaded Java, double checked locking <a href="http://www.javaworld.com/javaworld/jw-05-2001/jw-0525-double.html?page=1">isn't a silver bullet</a>. 

However, with the locking scheme that ColdFusion implements, it's a safe(r) way to make sure that something that should only happen once does, in fact, happen once.