ColdFusion double-checked locking demystified

I was talking to a coworker today about a strange exception we saw, and I mentioned that the lock surrounding the process may not be double checked. It drew a "What does that mean?," so I thought I'd blog the answer that shows a practical example.

[Read more on Joe Rinehart's blog]

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:

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

At first glance, the <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 <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 <cfif> says the framework isn't loaded, but there's a <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 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 <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 <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 <cfif> says the framework isn't loaded, but there's a <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. I'm going to load the framework, again, even though I just did so for Jim, and his request may still be running."

MG (to Jim): "Oops! 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 isn't a silver bullet.

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.

TweetBacks
Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
© 2018 Joe Rinehart
BlogCFC was created by Raymond Camden. This blog is running version 5.9.3.006.