The difference between application scope and request scope in coldfsuion

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
LVL 19
erikTsomikSystem Architect, CF programmer Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
erikTsomikSystem Architect, CF programmer Author Commented:
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?
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).
erikTsomikSystem Architect, CF programmer Author Commented:
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?
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..

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ColdFusion Language

From novice to tech pro — start learning today.