Solved

The difference between application scope and request scope in coldfsuion

Posted on 2014-09-09
5
190 Views
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
0
Comment
Question by:erikTsomik
  • 3
  • 2
5 Comments
 
LVL 39

Expert Comment

by:gdemaria
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
0
 
LVL 19

Author Comment

by:erikTsomik
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?
0
 
LVL 39

Expert Comment

by:gdemaria
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).
0
 
LVL 19

Author Comment

by:erikTsomik
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?
0
 
LVL 39

Accepted Solution

by:
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..
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
File names with & character 6 60
ip / url redirect 13 57
spamming  on Hosted svrs? 6 50
Public IP Address Amazon Servers 2 14
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

747 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now