• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Quick Question About Locking

New Here ,
Jun 22, 2010 Jun 22, 2010

Copy link to clipboard

Copied

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

TOPICS
Advanced techniques

Views

315

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Jun 23, 2010 Jun 23, 2010

Copy link to clipboard

Copied

LATEST

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation