Tuesday, February 09, 2010    
Home My Books Blog ColdFusion About Me Back    

Calendar
<< Feb 2010 >>
S M T W T F S
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28            

Search

Categories
 • Acrobat (3) [RSS]
 • Adobe (88) [RSS]
 • AdobeMAX06 (45) [RSS]
 • AdobeMAX07 (59) [RSS]
 • AdobeMAX08 (66) [RSS]
 • AdobeMAX09 (39) [RSS]
 • AdobeMAX10 (1) [RSS]
 • AIR (215) [RSS]
 • Appearances (190) [RSS]
 • Books (72) [RSS]
 • CFEclipse (15) [RSS]
 • ColdFusion (1371) [RSS]
 • Data Services (33) [RSS]
 • Fish Tank (4) [RSS]
 • Flash (179) [RSS]
 • Flex (493) [RSS]
 • Home Automation (4) [RSS]
 • Jobs (116) [RSS]
 • JRun (14) [RSS]
 • Labs (43) [RSS]
 • LiveCycle (34) [RSS]
 • MAX (232) [RSS]
 • Mobile (109) [RSS]
 • Regular Expressions (17) [RSS]
 • RIA (19) [RSS]
 • SQL (40) [RSS]
 • Stuff (532) [RSS]
 • Tips (CF Studio) (80) [RSS]
 • Tips (CF) (795) [RSS]
 • Tips (Dreamweaver) (91) [RSS]
 • Tips (Flex Builder) (2) [RSS]
 • Using CF (161) [RSS]

Other BLOGs
 • Charlie Arehart
 • Lee Brimelow
 • Ray Camden
 • Christophe Coenraets
 • Sean Corfield
 • Mihai Corlan
 • Cornel Creanga
 • Mark Doherty
 • John Dowdell
 • Danny Dura
 • Enrique Duvos
 • Steven Erat
 • Kevin Hoyt
 • Serge Jespers
 • Adam Lehman
 • Duane Nickull
 • Miti Pricope
 • Andrew Shorten
 • Ryan Stewart
 • James Ward
 • Greg Wilson
 • Full As A Goog

RSS Feeds
 • Feed
 • Subscribe

Join my mailing list and find out about new books and other topics of interest.

Thoughts, ideas, tips, musings, and pontifications (not necessarily in that order) by Ben Forta ...
NOTE: This is my personal blog, and the opinions and statements voiced here are my own.

Viewing By Entry / Main
April 26, 2004

Is Locking Still Needed in CFMX?

I receive several questions a week about locking, and the need for <CFLOCK> in ColdFusion MX, and so I thought it worthwhile to post a reply that I just sent to a user ...

Locking in ColdFusion, and the <CFLOCK> tag specifically, has two primary uses, and these must be distinguished. The first use of <CFLOCK> has nothing to do with variables or shared memory, and everything to do with single-threading events. For example, if you had code that wrote to a specific file using <CFFILE>, you'd never want two requests to write at the same time (or you'd risk trashing the file). You'd use a named lock <CFLOCK NAME=""> to ensure that only one write happened at a time. <CFLOCK> is used to protect any code or operation that is not multi-thread safe. (This also applies to many CFX tags, COM objects, and more). Obviously, this locking need is every bit as relevant in CFMX as it was previously.

The more common use of locking is to protect shared memory from corruption. Let me explain. There are three scopes in ColdFusion that may be accessed by more than one request at any given time, these are SERVER, APPLICATION, and SESSION. Prior to ColdFusion MX, ColdFusion had a serious limitation in that if one request attempted to read or write shared memory while another request was in the middle of a write, then memory corruption could occur. This was never an issue with concurrent reads, but any activity concurrent to a write could cause problems. And these problems were tough to diagnose, the results of memory corruption can be erratic, delayed, and very inconsistent. And so we all got used to locking shared memory access, using LOCK="exclusive" for writes and LOCK="readonly" for reads.

But preventing memory corruption is not the only reason to lock shared memory. There is actually another reason, one that has nothing to do with a ColdFusion limitation, and everything to do with multi-threaded applications in general. Consider the following scenario; you have ten variables in APPLICATION that your code is updating, and mid-update another request reads those variables so that it has five old values and five new values. Is that a problem? Maybe, and maybe not, it depends on your code and application needs. This is what is known as a "race condition", a situation where unpredictable behavior could result from the execution of code by two or more requests without proper sequencing. Race conditions are neither inherently bad nor good, but their results may cause problems for you depending on what your application is doing. If this type of condition is an issue for you, then you need to control execution sequence, and in ColdFusion this is achieved by using <CFLOCK> tags.

So, is the locking of shared memory still needed in ColdFusion MX? The answer is that it depends on why you are locking. ColdFusion MX does not suffer from the memory corruption that plagued prior versions, and so not locking will not harm ColdFusion at all. If the only reason that you ever locked shared memory was to prevent memory corruption, then no, there is no need to lock. But race conditions are still an issue, and if you are concerned about them then you still need to lock access to shared memory. Of course, race conditions may in fact be an issue that you never paid attention to because you were locking anyway (to prevent corruption).

The bottom line, locking is still needed for any code that is not multi-thread safe. And locking shared memory access is needed in order to prevent race conditions, if those are an issue. But locking merely to prevent memory corruption is, thankfully, a thing of the past.

TrackBacks
There are no trackbacks for this entry.

No trackback URL. Trackbacks are only allowed via interactive form.

Comments

  © Copyright 1997-2009 Ben Forta, All Rights Reserved