The difference between application scope and request scope in coldfsuion

Posted on 2014-09-09
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
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 the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

765 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