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

Creating objects in application scope or session scope?

New Here ,
Apr 26, 2012 Apr 26, 2012

Copy link to clipboard

Copied

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

Views

2.8K

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 ,
Apr 26, 2012 Apr 26, 2012

Copy link to clipboard

Copied

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. 

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
New Here ,
Apr 27, 2012 Apr 27, 2012

Copy link to clipboard

Copied

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?

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
Advocate ,
Apr 27, 2012 Apr 27, 2012

Copy link to clipboard

Copied

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


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
Community Expert ,
Apr 27, 2012 Apr 27, 2012

Copy link to clipboard

Copied

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.

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
Community Expert ,
Apr 27, 2012 Apr 27, 2012

Copy link to clipboard

Copied

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.

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
New Here ,
Apr 30, 2012 Apr 30, 2012

Copy link to clipboard

Copied

Thanks guys. I have another question which is also about creating objects and the application scope. In a database I have a table with configuration parameters which I want to provide in an application variable. I see two alternatives.

In the first one I explicit create an object and call the function:

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

In the second one I call directly the function:

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

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 ,
Apr 30, 2012 Apr 30, 2012

Copy link to clipboard

Copied

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.

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
Community Expert ,
Apr 30, 2012 Apr 30, 2012

Copy link to clipboard

Copied

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.

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
New Here ,
May 03, 2012 May 03, 2012

Copy link to clipboard

Copied

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?

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

    <cfset tblConfiguration = "tblConf">

    <cfset APPLICATION.configObject = new components.config()>

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


    <cfreturn true>

</cffunction>


Lifetime of configObject = Lifetime of application

Scope of configObject = application

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

    <cfset tblConfiguration = "tblConf">

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

    <cfreturn true>

</cffunction>

Lifetime of configObject = very short, after calling the function, the temp. object will be destroyed (like cfinvoke)

Scope of configObject = variables, but only a very short time

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
Community Expert ,
May 03, 2012 May 03, 2012

Copy link to clipboard

Copied

LATEST

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

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