The difference between application scope and request scope in coldfsuion

Posted on 2014-09-09
Last Modified: 2014-09-16
What is the difference between application scope and request scope in coldfsuion . Please show me all pros and cons. I read Adobe site but still not clear
Question by:erikTsomik
  • 3
  • 2
LVL 39

Expert Comment

ID: 40312450
The application scope is a shared scope that persists until the application timeout (similar to session timeout).   All users will see the same value.  If user A changes the value, user b will see the change.   The best use for application scope is to define global variables such as the datasource for your query, that is set once for everyone and never changed.

The request scope is a global variable that does not persist.   It disappears after the page is loaded.   The scope is only for each user and the only for the one request.     So you could set the value at top of the page and it will persist until the page is finished.   A good use for this is to set the test/debug mode for example.    If you want one user to be in test mode and see some things no one else will see, set their variable  request.mode = "test" and all CF templates and cffunctions can see this variable and know to do something different depending on test or live.... just an example
LVL 19

Author Comment

ID: 40312603
Thank you . Also often times I do see that the datasource is being declared in a request scope. Is that a right approach to take or the application scope is the best thing to do.

Also if the request scope persist only one request can then variables   be declares in the variable scope or local scope?
LVL 39

Expert Comment

ID: 40312628
it is fine to define the datasource in the request scope, you just need to do that onRequestStart which will set the variable every time for every user.   Seems a bit more efficient to set it once for all users.   But if it will ever change or varies by user, then request is the better option as application scope is one variable for everyone and should not change.

> Also if the request scope persist only one request can then variables   be declares in the variable scope or local scope?
Not sure I understand the question, if I don't answer it right, feel free to ask again.

The variables scope should be for local use variables and these variables really should not be used from one template to another (that is, you should not set  variables.FirstName in one CF template and then exect to use it in another template).   That is very possible just not the best coding practice in my opinion.   Use request scope sparingly, only when you have a variable that you want to persist throughout the entire application.   One thing that I do is to fetch the session.user_ID from the login and assign it immediately into request.user_id the global variable.  I do this in application.cfc because I would rather refererence request.user_id everyone instead of the session variable everywhere.  That is one use of a global variable that may change on each page load (the user may logout).
LVL 19

Author Comment

ID: 40312684
It makes my understanding a little bit better . Can you reference a few more examples of how and where to use request scope. And the benefits over application scope variable?
LVL 39

Accepted Solution

gdemaria earned 500 total points
ID: 40312744
Well, the use of application scope is very limited.   You would not want to use it anytime when a value may change often and you would not want to use it when one user would have a different value from another user.    For example, if you used application.user_id to track someone's login, the value would change every time another person logged in and it would log off every user except one.   There is only ONE instance of the value and that value is shared by everyone, so there are not a lot of things you do with it.  You could set a variable to defined the path to your image folder, because that path would be the same for all users and would never change.

Request scope is very different.   The value exists for only one user and ONE request (page load).  Then the value is gone.   So it is the global variable you typically think of.   But using a global variable too often is not good!   Since the value of the request scope does not persist between page loads, you cannot use it to maintain a login, or to keep track of a shopping cart - unless you set the value EVERYTIME a page is requested.     I don't use request scope a lot because I don't think it's very good programming to define global variable, it's much better to pass variables such as passing to a cffunction.   But I think it can be used for some things such as "switches" or user preferences.   For example, in the application.cfc file you could fetch the users' preferences from the database and load them into request scope variables..    Which color scheme does the user want for their page?   What language do they use?   What privileges does the user have?  These type of settings are global, can vary by user and may change because the user changes their minds and wants a different color or may be granted different priviledges..

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Printing to a specific printer tray (HP 806dn printer) 3 101
Form submit issue 11 85
ColdFusion 10 Error 2 50
Stream live video from Raspberry Pi camera 22 161
Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question