[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Understanding scoping and rescoping of varaiables

Posted on 2007-10-19
2
Medium Priority
?
594 Views
Last Modified: 2013-12-24
I would like to understand the concept of scoping and re scoping of variables.

Say in the application.cfm, I set an appliation variable for data source called "application.dsn". I am working on somebody elses code and I see this in the next block.

<!--- Rescope application variables to avoid potential read collision --->
<cflock timeout="20" throwontimeout="yes" type="readonly" scope="application">
  <cfset request.dsn =          duplicate(application.dsn)>
</cflock>

Questions:
1. What would happen if I don't create the request variable that stores the value of the application variable?
2. Why is he using duplicate function to pass the value to the request variable? Why cant I just do "request.dsn =application.dsn
3. Wondering where read conflicts normally occur? Say there is a session variable that stores "userid". I see the developer even do a request scope for session.userId

I am basically trying to understand the concept of rescoping.

Tx in advance.


0
Comment
Question by:dynamota
2 Comments
 
LVL 14

Accepted Solution

by:
Scott Bennett earned 2000 total points
ID: 20111828
1. What would happen if I don't create the request variable that stores the value of the application variable?

This makes it so the application scope is only locked once during the request. If you don't do this then you would have to reference the application scope everytime you used the variable (in this case, everytime you ran a query).

2. Why is he using duplicate function to pass the value to the request variable? Why cant I just do "request.dsn =application.dsn

you could do it either way, but I belive duplicate was more usefull back in older versions (before 5) where when you did code like:
 <cfset a = 1>
<cfset b = a>
<cfset b = 2>
<cfoutput>#a#</cfoutput>
the output would be 2 because <cfset b=a> actually just set up a reference to the variable a and anything you did to the one would effect the other, back then you could use <cfset b = duplicate(a)> or <cfset b = #a#> to ensure that changes to b did not effect a. as of CF 5 it is no longer neccessary because the behavior changed.

3. Wondering where read conflicts normally occur? Say there is a session variable that stores "userid". I see the developer even do a request scope for session.userId

This seems unneccessary as session scope locks will only effect the 1 session where as the applications locks would effect everyone., but he was probably trying to do the same thing as in the first question. I usually just check the single threaded sessions box in the coldfusiond administrator settings and then don't bother with session scope locking at all.

0
 
LVL 1

Author Comment

by:dynamota
ID: 20113550
SBennet,

Thank you very much explaining this. Its very clear now.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authori…
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…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

829 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