Skip to main content
Inspiring
February 17, 2009
Question

Locking when using a component as an object in the application scope...

  • February 17, 2009
  • 2 replies
  • 351 views
I have a component that I am building right now that hold application settings that are stored in a database table. The settings are maintained in a structure "variables.settings" within the component and can only be accessed by get and set methods. I use the following to create the instance of the object:

<cfset application.settings = createObject("settings","component").init() />

Now when getting a setting I do not think locking is needed as its just reading the value and I am not really concerned with a race condition...

But when using the set method which will update the value of the setting.... should I be locking that as technically the object is in a shared variable scope? Or is it safe because its within the cfc/object?

If locking is needed, would I need to lock when using the set method? or can I just build the lock into the set method so that its always there?



    This topic has been closed for replies.

    2 replies

    Inspiring
    February 17, 2009
    To disagree with craigkaminsky, I think you only need to lock if you are
    concerned about race conditions and what could happen during a 'dirty
    read' and|or conflicting writes.

    A lot of developers have an old impression of ColdFusion that one *must*
    lock all shared scope variable access to maintain a stable application.
    This dates from the ColdFusion 4.5 days where there where bugs in
    ColdFusion that could cause memory leaks and eventual application
    instability if one did not lock shared scope reads and writes. This has
    long been fixed, but the advice persists.

    So now it is only a matter of your data and what would happen if one
    user read an old value while it was in the process of being updated by
    another user. Or could two users be updating the same value at the same
    time and cause conflict. If either of those two statements are true,
    then yes you should use locking as required by your application.

    But if they are both false and it does not matter that user A might get
    the old value microseconds before user B changes it. Or there is no
    concern with user A changing it once and user B changing it again to
    something different moments later without knowing the user A has already
    changed it. Then locking is probably unnecessary.

    There can be a cost to over locking shared variable writes and|or reads.
    Every time one creates a lock, one is single threading some portion of
    ones code. Depending on how the locking is done, this single threading
    may only apply to individual users or it may apply to every single user
    on the entire server. Either way, too much of this in the wrong places
    can create a significant bottle necks in ones application if too many
    user requests start piling up waiting for their turn through the locked
    block of code.

    Inspiring
    February 17, 2009
    Hi, josheby,

    I think it's always best to use cflock when writing to something in the application scope. It's pretty simple to pull off:

    <cflock scope="application" timeout="10" type="exclusive">
    <!--- call your set method here --->
    <cfscript>
    application.settings.setSettings();//or whatever your method is called :)
    </cfscript>
    </cflock>

    You can also lock the read from your settings using the same cflock tag as above but changing the type attribute to "readonly".

    When type is set to "readonly" ColdFusion will allow more than one request to read the data. While "exclusive" will ensure that only one request can write the data at a time.

    The use of an exclusive lock is, in my opinion, crucial but I think use of the readonly lock is open to debate (I use it when reading from shared scopes but have never compared how an application fares with this lock in place versus without it in place).

    HTH!