1 Reply Latest reply on Jun 23, 2010 6:43 AM by ilssac

    Quick Question About Locking

    gregmoser

      I have a CFC that i create and store in the application scope.  It looks like this

       

      <cflock scope="application" type="exclusive" timeout="10">

           <cfset application.utilityfunctions = createObject("component", "cfcs.utilityfunctions") />

      </cflock>

       

      Now my question... do i need to lock the application scope when using one of the methods in my component? or not?

       

      <cfoutput>

           <cflock scope="applicaition" type="readOnly" timeout="10">

                #application.utilityfunctions.getCoolStringFunctionTool(myinputstring)#

           </cflock>

      </cfoutput>

       

      Or can i simply use this

       

      <cfoutput>

           #application.utilityfunctions.getCoolStringFunctionTool(myinputstring)#

      </cfoutput>

       

      Thanks,

      Greg

        • 1. Re: Quick Question About Locking
          ilssac Level 5

          I don't know, do you?

           

          The reason you would lock the code is because there is a danger of a race condition.  And whether there is a danger or not, depends on what the functions do and how you have written them.

           

          The question to answer here is what happens if two or more people are using your application and they cause the component method to be executed at the same time.  Is this going to cause problems, then you may need to lock the code or refractor the function so that it does not cause a problem.  If it does not cause a problem, then locking is not necessary.

           

          There is a tendency to over lock in ColdFusion left over from bugs that existed in the version 4|4.5 of CF.  This has been resolved for a decade now, but you still see lots of advice to always lock shared scope variables like you have.  Nowadays you only need to lock if there is a race condition reason in your application.  There is a performance cost to locking code.  Wherever your code is locked, it is single threaded.  That means only one request at a time can execute the locked code, that is the purpose of locking it.  That can incorporate both a slow down as requests possibly pile up waiting their turns to execute the locked code OR possible deadlocks where two or more pieces of code are waiting for other code to finish a locked segment that is also waiting for the first piece to finish.

           

          So, I would say it is likely that you do not need to lock that access to the methods OR the initialization that you are already locking, but only knowledge of the exact code can answer that definitively.