3 Replies Latest reply on Jul 30, 2008 3:37 AM by am2605

    Component in Application Scope and cflock


      I have an CFC that I set in application scope in the onApplicationStart method which contains some system parameters, one of which is a flag saying whether the site is currently "Online". I call a method on this CFC in the Application.cfc "onRequest" method to see if the flag value has changed.

      This method looks up a parameter set in the database that says whether the site is "online" or "offline". If the site is currently online, I want to only redo the query every 5 mins. However if the site is offline, I want to check more frequently (every 5 secs) as to whether the flag has changed back.

      What I am not sure of, is whether I need to be using <cflock> when I change from "online" to "offline"? There is only a single instance of this object, but what happens if multiple requests are calling the "isSiteOnline" method simultaneously? Is this "safe"?

      Some code snippets are attached to illustrate.

      Any advice would be greatly appreciated.

        • 1. Re: Component in Application Scope and cflock
          BKBK Adobe Community Professional & MVP
          <!--- DO I NEED TO USE CFLOCK HERE???? --->

          <!--- AND DO I NEED TO USE CFLOCK HERE???? --->

          Even though requests share the instance application.systemOptions, locks are unnecessary. When one request makes the call application.systemOptions.isSiteOnline(), it is independent of another request's call.

          • 2. Re: Component in Application Scope and cflock
            Level 7
            > <!--- DO I NEED TO USE CFLOCK HERE???? --->

            No, in either situation.

            Ask yourself... what would the ramifications be of two requests hitting
            that line of code "simultaneously"? The end result is that both of them
            are setting the variable to the same static value... so that's what's going
            to end up happening: variables.instance.cacheInterval is going to be set to
            300 (or 5) in each situation.

            You could well with to lock a block of code which - if called
            simultaneously via more than one request - could act on shared storage
            space (server, application or session data) differently and incorrectly
            than is intended.

            <!--- application bootstrap process --->
            <cfif not structKeyExists(application, isInitialised and not
            <!--- initialisation process, whatever it is --->

            <cfif allOK>
            <cfset application.isInitialised = true>
            <cfset application.isInitialised = false>

            So a sequence of events could be (say the user is hitting the site with two
            different browsers):

            REQUEST1: application.isInitialised doesn't exist
            REQUEST1: starts the init process
            REQUEST2: application.isInitialised STILL doesn't exist
            REQUEST2: starts the init process *again*
            REQUEST1: sets the application.isInitialised value
            REQUEST2: sets the application.isInitialised value

            Obviously one does not want two request running the initialisation code.

            note this is a slightly contrived example, as if one has an
            OnApplicationStart() method, then it will only run once, and that's where
            one would have the init code, but it demonstrates the issue.

            Google "race condition". Those are the situations in which one needs to
            lock blocks of code.

            • 3. Re: Component in Application Scope and cflock
              am2605 Level 1
              Thank you for your replies. That makes it a lot clear. Much appreciated.