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

Component in Application Scope and cflock

Explorer ,
Jul 29, 2008 Jul 29, 2008

Copy link to clipboard

Copied

Hi,

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.

Regards,
Andrew.
TOPICS
Advanced techniques

Views

429

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

correct answers 1 Correct answer

LEGEND , Jul 30, 2008 Jul 30, 2008
> <!--- 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 reque...

Votes

Translate

Translate
Community Expert ,
Jul 29, 2008 Jul 29, 2008

Copy link to clipboard

Copied

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

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

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.



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
LEGEND ,
Jul 30, 2008 Jul 30, 2008

Copy link to clipboard

Copied

> <!--- 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.

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

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

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.


--
Adam

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
Explorer ,
Jul 30, 2008 Jul 30, 2008

Copy link to clipboard

Copied

LATEST
Thank you for your replies. That makes it a lot clear. Much appreciated.

Regards,.
Andrew.

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