Last posts:
Web Design » Tutorials » ColdFusion double-checked locking demystified
ColdFusion double-checked locking demystified
Views: 588 | 1-11-2010, 08:05 | Category : Tutorials

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.

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 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 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 says the framework isn't loaded, but there's a 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
Jim: "Send me index.cfm."

MG (to Jim): "One moment, my first 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 says the framework isn't loaded, but there's a 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.


Tags: ColdFusion MX, Practices



Read also:
  • Clarified: CF (non OOP OOP) and ASP.NET (anti-OOP OOP)
  • Website offer
  • CFCUnit + FlexUnit How-To: No excuses now!
  • I Don't Like Rails
  • Rails' ActiveRecord, Reactor, Illudium: All backwords for OO.

  • Vote
    What do you need?

    Website design
    Website development
    PHP Tutorials
    Other


    Hire Desk
    »
    Website Design & Development, Php Tutorials
    This is Web developer Blog
    Only high quality web developming service