Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8853479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:39:50+00:00 2026-06-14T13:39:50+00:00

My current application is using single instance of an object as a global variable

  • 0

My current application is using single instance of an object as a global variable for many of the main components, which I understand is considered inferior to using dependency injection.

I wish to make my applications open source in the future, but first I want to refactor the code to use the most recommended techniques for team collaboration so that other developers will be able to change my source code more easily.

Example of a shared resource: In the CFML language, you have the Server scope, which is shared memory that is available to any request for the entire server instance.

Here is my new design concept for managing changes to the Server scope:

  1. Create a single instance of component named ServerMemoryManager which provides an interface for writing and reading to the server scope.
  2. Any other code that needs to access the server scope will be injected with a reference to the single instance of the ServerMemoryManager via an init() function or a setServerMemoryManager() function.
  3. Whenever a component reads/writes data to the ServerMemoryManager object, it will be able to internally lock the server scope so that no 2 threads can simultaneous write to the same piece of memory in the server scope.

Is this the best way to manage a shared resource (shared memory, filesystem, etc) that requires locking in order to be thread-safe?

Please describe any additional methods that can be used to manage a shared resource that requires locking during certain read/write operations which are considered best practices.

Edit: Based on the accepted answer, instead of locking scope=”server”, I will use named locks and manage the shared resources with more fine-grained locking. This may allow using multiple objects to manage the shared resources assuming they are all managing different keys in shared memory or files in the filesystem. For example, one application could have its own unique key or directory assigned to it so that it wouldn’t conflict with another application trying to change a shared resource.

Edit2: I found I could use a single component named scope.cfc for each scope if I pass the scope into the init function when I create the object. I am now using fine-grained named locks. Let me know if it can be improved. The actual revised code now looks like this (I excluded the code for read, delete, clear). It also doesn’t seem that it is required to have a single instance of the scope.cfc component anymore.

            <cfcomponent>
                <cfscript>
                variables.scope=false;
                variables.scopeName=false;
                </cfscript>
                <cffunction name="init" access="public" output="no" returntype="scope">
                    <cfargument name="scope" type="struct" required="yes">
                    <cfargument name="scopeName" type="string" required="yes">
                    <cfscript>
                    variables.scope=arguments.scope;
                    variables.scopeName=arguments.scopeName;
                    return this;
                    </cfscript>
                </cffunction>
                <cffunction name="write" access="public" output="no" returntype="boolean">
                    <cfargument name="key" type="string" required="yes">
                    <cfargument name="value" type="any" requires="yes">
                    <cfargument name="timeout" type="numeric" required="no" default="10">
                    <cftry>
                        <cflock type="exclusive" name="zcore-#variables.scopeName#-scope-#arguments.key#" timeout="#arguments.timeout#" throwontimeout="yes">
                            <cfscript>
                            variables.scope[arguments.key]=arguments.value;
                            </cfscript>
                        </cflock>
                        <cfcatch type="lock"><cfreturn false></cfcatch>
                    </cftry>
                    <cfreturn true>
                </cffunction>
            </cfcomponent>

** Edit3:** I tested the performance of reading from server scope through a component method like this and found it to be 20 times slower then reading the server scope directly when using a read only lock and 4 times slower without a lock. The overhead of an extra function call hundreds or thousands of times per request will be too slow. Tests done on Railo 3.3.x.

I prefer to build a large object in a non-public request and then set a single shared memory scope key then try to write an incomplete object to the scopes. Example:

<cfscript>
ts=structnew();
ts.largeObject=buildLargeObject();
server.cachedObject=ts;
</cfscript>

This lets you avoid locking across the entire application when you only write complete objects to shared memory since updating a single struct key is thread-safe. However, when you build the large object on startup, you need to be sure it is locked until that object is fully created.

I’m going to make the scope variable become directly readable by using the this scope instead of variables scope in the init function to avoid slowing down the application.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T13:39:52+00:00Added an answer on June 14, 2026 at 1:39 pm

    CFLOCK only prevents code from executing if every occurrence is locked the same way.

    For example:

    page1.cfm

    <cflock type="exclusive" scope="server" timeout="10" >
       <cfset application.xyz = 'abc'>
    </cflock>
    

    page2.cfm

    <cfset application.xyz = '123'>
    

    Page2.cfm is going to negate any locks you have on page1.cfm if page2 runs the same time page1 does. That said, it’s good that you are locking inside your cfc so that each object doesn’t have to be locked.

    However, locking every occurrence isn’t enough. The following won’t do much good either.

    page1.cfm

    <cflock type="exclusive" scope="server" timeout="10" >
       <cfset application.xyz = 'abc'>
    </cflock>
    

    page2.cfm

    <cflock type="exclusive" scope="server" timeout="10" >
       <cfset application.xyz = '123'>
    </cflock>
    

    This will halt the processing for every request of page1 and page2 but will not protect application.xyz on page1 from changes made to application.xyz on page2. To do this you need to give your locks a “name”.

    Locks name. Mutually exclusive with the scope attribute. Only one
    request can execute the code within a cflocktag with a given name at a
    time. Cannot be an empty string.

    Permits synchronizing access to resources from different parts of an
    application. Lock names are global to a ColdFusion server. They are
    shared among applications and user sessions, but not clustered
    servers.

    Because you are creating multiple instances of your object, I believe serverMemoryManagerObject could interfere with serverMemoryManagerObject2 unless you name your lock.

    Here is some more information about locking dos and don’ts

    • Locking code with cflock
    • CFLock And Negative Outcomes Think It Through
    • CFMythbusters – Countering Some Conventional Wisdom
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I understand how to force a single instance of an application using a mutex
I have been building a new application using my current understanding of domain driven
I am able to display the current time on my iPad application using the
I'm able to retrieve the current location in my iPad application using, CLLocation *location
I'm in a ASP.NET application using Windows Authentication. I'm using HttpContext.Current.User.Identity.Name to get the
I am using Asp.Net/C# in my application.My current situation is that I am using
I am implementing a comet using AsyncHttpHandlers in my current asp.net application. According to
I am using FakeGps an application that spoofs your current location. I want to
I am using finish() to close current activity before quit application in Android. However,
I am using WebDateChooser in my .net application...by default the Date Chooser shows Current

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.