Skip navigation
domgghghgh
Currently Being Moderated

Creating objects in application scope or session scope?

Apr 26, 2012 7:25 AM

Hi there

I have a queries.cfc with functions which are getting or setting fields in a database. The application runs on CF 9 and has a lot of parallel users. To speed up the function calls I create the object in the Application.cfc like this:

 

 

<cffunction name="onSessionStart" returntype="boolean" output="true">

 

     <cfset structDelete(session, "queriesObject")>

     <cfset SESSION.queriesObject = new components.queries() >

 

     <cfreturn true>

</cffunction>

 

What is the better way to create the object: in the application or session scope? In the application scope I would create the object once and in the session scope I would create the object for every user. That needs more memory but every user has an own object.

What is about the locking mechanism if multiple users call the same function from the object?

 

Thanks for answers and discussions...

 
Replies
  • Currently Being Moderated
    Apr 26, 2012 7:53 AM   in reply to domgghghgh

    If the queryobject is the same for every user, and the cfc does not have any global variables that the user can change, the application scope is appropriate. 

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 27, 2012 7:31 AM   in reply to domgghghgh

    >> Does coldfusion generate for every user/request an instance of this object? If no, isn't it a bottleneck concerning performance?

     

    No and No

     

    1. You are creating the instance and sticking it in the App scope. ColdFusion does not create it again when you call it.

     

    2. ColdFusion is multi-threaded, so if two users make a request to the same function in the object they will both run in parralel. Which is why it is VERY important to properly scope your function variables (var them). Unless it is a long running function and you have lots of simultaneous users, I doubt you would experience any performance issues.

     

    Jason

     


     
    |
    Mark as:
  • Currently Being Moderated
    Apr 27, 2012 8:46 AM   in reply to domgghghgh

    domgghghgh wrote:

     

    What is the better way to create the object: in the application or session scope? In the application scope I would create the object once and in the session scope I would create the object for every user. That needs more memory but every user has an own object.

    This shouldn't sound like a choice between programming styles. It is your requirements or specifications that determine the choice.

     

    If, as Dan says, nothing about the component's variables changes from one user-session to the next, then you should store the object in the application scope (in onApplicationStart). If the component has any dependency on the user or session, the session scope and onSessionStart would be more appropriate.

     

    What is about the locking mechanism if multiple users call the same function from the object?

     

    You need no lock for application scoped code within onApplicationStart or for session scoped code within onSessionStart.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 27, 2012 9:16 AM   in reply to domgghghgh

    domgghghgh wrote:

     

    Thanks for your answer Dan. Do you know what exactly happens with this global obejct in the application scope if multiple users access the same function? Does coldfusion generate for every user/request an instance of this object? If no, isn't it a bottleneck concerning performance?

    On the contrary, the more instances (such as when they are stored in the session scope), the higher the chances of a bottleneck.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 30, 2012 5:04 AM   in reply to domgghghgh

    First, you don't need structDelete.  The function is onApplicationStart so the structure can't possibly exist.

     

    In your first example, the configObject stays in memory for the life of the application.  In the second, whatever object is created goes away.  If you don't need the object, your second method is more efficient.

     

    As an aside, because one can never have too many choices, the cfinvoke tag is equivalent to your second method.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 30, 2012 5:37 AM   in reply to domgghghgh

    domgghghgh wrote:

    <cffunction name="onApplicationStart" returntype="boolean" output="true">

     

        <cfset tblConfiguration = "tblConf">

        <cfset structDelete(application, "configObject")>

        <cfset configObject = new components.config()>

        <cfset APPLICATION.Configuration = configObject.getConfiguration(tblConfiguration)> <!--- function returns a struct --->


        <cfreturn true>

    </cffunction>

     

    What is the lifetime and the scope of the configObject in that case without <cfset var configObject... ?

    Lifetime = the duration of the call to onApplicationStart (usually infinitesimally small), for example, when the application starts.

    Scope = variables.

     

    <cffunction name="onApplicationStart" returntype="boolean" output="true">

     

        <cfset tblConfiguration = "tblConf">

        <cfset structDelete(application, "Configuration")>

        <cfset APPLICATION.Configuration = new components.config().getConfiguration(tblConfiguration)> <!--- function returns a struct --->

     

        <cfreturn true>

    </cffunction>

     

     

    I guess that Coldfusion has to create the object to call the function getConfiguration. Is it only a temporarily object? What is the lifetime of this object? I am really interested in the the lifetimes of objects in CF to understand better the mechanism.


     

    Yes, components.config() is a temporary object. There is no difference with the prior example. Its lifetime is the duration of the onApplicationStart event.

     

    However, remember that onApplicationStart need not only run at the beginning of the application. If you place the line <cfset onApplicationStart()> in onRequestStart, then the object components.config() will be defined at every request.

     
    |
    Mark as:
  • Currently Being Moderated
    May 3, 2012 11:55 PM   in reply to domgghghgh

    domgghghgh wrote:

     

    I've modified the first example and have written for both the lifetime and scope of the object. What do you think about this, is it correct?

    Yes. At least, in my opinion. But there is one exception. if you write

     

    x = new components.config();

     

    then object x is in variables scope. However, if you write

     

    new components.config().someFunction();

     

    then  new components.config() is not in variables scope.

     

    Message edited by: BKBK

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points