|By Charlie Arehart||
|July 11, 2001 12:00 AM EDT||
You've probably seen the use of a variable called "application.dsn" (or "application.datasource") in code. Perhaps you've even been taught to use the method in a class.
Maybe you've even been doing it in your own code. I'm talking about setting this variable in the application.cfm file to hold the name of your data source for a given application.
It seems so innocuous, and it seems to provide ways to make your code easier to maintain (change the dsn variable once in the application.cfm, and all the templates that use it under control of that application.cfm get the benefit of the change).
However, there's a problem and it can be a nasty one. This issue has to do with the locking of (or failure to lock) shared-scope variables such as this one, and the fact that rarely does any discussion of this approach include the consequences of using shared variable scopes. There have been other articles in CFDJ on the subject of shared variable locking, as well as Macromedia Knowledge Base articles.
Maybe you didn't make the connection between them and this issue. This article puts the problem in perspective and offers some explanations of how to understand and resolve it. The ultimate solution involves using a "request"-scoped variable instead of an "application"-scoped one.
If you're not familiar with shared variable scopes, or are fuzzy about locking issues, or perhaps have never understood what "request" scope variables are about, this article should help you.
If you do understand these things, change your references to application.dsn to request.dsn, and if you find any locks around code that was using such an application.dsn variable, you need to consider whether those should stay as well. I'll also offer insights into how to find and fix such locking references.
You may see code doing this in application.cfm:
<CFSET application.dsn = "whatever">
which declares the variable as "global," in essence, and can therefore be used later in all other templates as in:
<CFQUERY datasource="#application.dsn#" ... >
to refer to that data source name. The upside to this is that if the data source name needs to be changed (from "whatever" to "whatever_test", for instance), you can simply modify the application.cfm to point to the new name, and all templates under its control get the benefit of the change.
It's a good plan, but the use of an application-scoped variable is flawed. It opens you to potentially troublesome locking issues (for more on that, including a good explanation for why it's a problem, see "ColdFusion Locking Best Practices" at www.allaire.com/Handlers/index.cfm?ID=20370&Method=Full). More important, you can have the intended benefit with an equally useful and less troublesome way.
For reasons I'll explain in a moment if it's still unclear, I'm suggesting that you stop using the application scope to hold the data source name. However, instead of dropping "application." from the variable name, I'm suggesting that you use the "request" scope. In other words, do the following (in your application.cfm):
<CFSET request.dsn = "whatever">
and then in all your templates do:
<CFQUERY datasource="#request.dsn#" ...>
If it's not clear why this is useful, or how it works, then there may be confusion about:
- How the application.cfm works like a CFINCLUDE (and how we could, but won't, use a local variable called "dsn")
- What the request scope is about (and why it's better to use "request.dsn")
How Application.cfm Works Like a CFINCLUDE
And How We Could, but Won't, Use a Local Variable Called "dsn"
Nearly every CF developer knows that whenever a CF template is run, CF first tries to execute any application.cfm that exists in the same directory (or its parent directory, or its grandparent, and so on).
What may not be obvious is that CF actually runs the application.cfm like a CFINCLUDE, which means that any variables set there, including "local" variables (such as <CFSET firstname="bob">), are then available to the template that was originally being run.
So let's say we have a template that does the following:
If it did this and nothing else, you might expect it to fail since you can't refer to variables that don't exist, and it's only outputting the variable, not creating it. But if firstname was set in application.cfm (assuming this template is in a directory controlled by that application.cfm), it can indeed refer to the variable.
Knowing that, you may wonder why the folks who promoted this solution of setting the dsn variable to the application scope even bothered. They could just as easily have said:
<CFSET dsn = "whatever">
and then in all their templates do:
<CFQUERY datasource="#dsn#" ...>
It would work. In fact, there's no need to be using the application scope to pass the variable to all templates (make it global), because any variables set in the application.cfm "trickle down" to all templates, in effect making them global. There are certainly good uses of application scoped variables, but this isn't one of them.
The simple example of setting a variable called "dsn" (or what could be formally specified as variables.dsn, which is the same thing) to hold the name of the data source would work, and would trickle down to all the templates. It's effectively "global," at least for the life of that template, and it's reset at the execution of each template by being executed in the application.cfm each time.
I've recommended that you use "request.dsn" rather than "dsn" or "variables.dsn". Why? And what is the request scope, anyway?
What the Request Scope Is About
And Why It's Better to Use "Request.dsn"
If you understand that setting a local variable called "dsn" will work, can you think of any situation in which your code may expect to have access to that variable but won't? Think hard. Okay, custom tags. Yep, most know that custom tags have their own local variable scope. So a local variable set in the caller (or in the application.cfm) won't be available to the custom tag.
When we were setting the dsn variable to an application scope, it was available in the custom tag (as are all shared scopes and also form, url, cgi, and other variables that are passed to the calling template).
By changing the application.dsn variable to dsn (or variables.dsn, same thing) while it's available in all templates under control of the application.cfm, it's not available to any custom tags called by those templates. That's why we need to use the request scope instead.
Its sole purpose, poorly understood though it is, is to create local variables in a program that are available within custom tags called by that program (and vice versa). That's it. Nothing more.
Many confuse the request scope with some sort of persistence or shared nature (and the fact that there's a different kind of "request" scope in other languages like ASP and JSP only confuses matters further). It's best to think of it as nothing more than a scope that allows local variables to be seen in a custom tag called by the template setting the request scope variable. And since our request.dsn variable is set in the application.cfm, it trickles down to the template being executed and is therefore also available to any custom tags we call as well.
That's why you should use the request scope rather than a local scope.
Remediation of Application.dsn Misuse
One last thought: you may not be able to blindly do a global search and replace application.dsn with request.dsn wherever it occurs. Depending on the savvy of the coder, the use of application.dsn may have at least two wrinkles that require more care than just replacing application.dsn with request.dsn:
- You may be testing for the existence of application.dsn before setting it in the application.cfm, in which case you need to set the request.dsn outside that test since it will never "already exist."
- You may have CFLOCKs surrounding CFQUERYs, or CFLOCKs that are moving the application.dsn to a local variable. You wouldn't want to just rename the scope in those instances.
I hope this article not only helps prevent problems with application.dsn, specifically, but also increases your understanding of request scope variables and application.cfm processing in general.
|charlie arehart 04/15/08 02:16:10 PM EDT|
Thanks for that, Brian. I should add that while this article was from 2001, since CF7 we now have application.cfc and its onapplicationstart method. That does change things a bit, in that one could safely create an application variable there and it really would be created only once, at the start of the application. For that reason, I suppose it may no longer be so "wrong" to see application.dsn--but again, that's only if you're using this feature. Otherwise, it may be better to stick with the request scope as discussed in the article.
|Brian 04/04/08 03:17:23 PM EDT|
Very informative! I've been wondering what the request scope is for such a long time and no other resource did such a great job of explaining it.
|Charlie Arehart 08/31/01 10:36:00 AM EDT|
A read asked by email: "don't you also need to lock request vars?" The answer is no. They're not shared by any other user. More particularly, they're not shared by any other "thread".
The request scope, like the variables scope, is local to the running program and belongs only to it. It's a single thread of execution, so there's no risk of conflict with other threads, as with the "shared scope" variables, server, application, and session.
In his session at @ThingsExpo, Chris Klein, CEO and Co-founder of Rachio, will discuss next generation communities that are using IoT to create more sustainable, intelligent communities. One example is Sterling Ranch, a 10,000 home development that – with the help of Siemens – will integrate IoT technology into the community to provide residents with energy and water savings as well as intelligent security. Everything from stop lights to sprinkler systems to building infrastructures will run ef...
May. 4, 2016 07:00 PM EDT Reads: 1,268
Artificial Intelligence has the potential to massively disrupt IoT. In his session at 18th Cloud Expo, AJ Abdallat, CEO of Beyond AI, will discuss what the five main drivers are in Artificial Intelligence that could shape the future of the Internet of Things. AJ Abdallat is CEO of Beyond AI. He has over 20 years of management experience in the fields of artificial intelligence, sensors, instruments, devices and software for telecommunications, life sciences, environmental monitoring, process...
May. 4, 2016 05:30 PM EDT Reads: 1,380
The demand for organizations to expand their infrastructure to multiple IT environments like the cloud, on-premise, mobile, bring your own device (BYOD) and the Internet of Things (IoT) continues to grow. As this hybrid infrastructure increases, the challenge to monitor the security of these systems increases in volume and complexity. In his session at 18th Cloud Expo, Stephen Coty, Chief Security Evangelist at Alert Logic, will show how properly configured and managed security architecture can...
May. 4, 2016 05:15 PM EDT Reads: 383
We’ve worked with dozens of early adopters across numerous industries and will debunk common misperceptions, which starts with understanding that many of the connected products we’ll use over the next 5 years are already products, they’re just not yet connected. With an IoT product, time-in-market provides much more essential feedback than ever before. Innovation comes from what you do with the data that the connected product provides in order to enhance the customer experience and optimize busi...
May. 4, 2016 05:00 PM EDT Reads: 1,318
A critical component of any IoT project is the back-end systems that capture data from remote IoT devices and structure it in a way to answer useful questions. Traditional data warehouse and analytical systems are mature technologies that can be used to handle large data sets, but they are not well suited to many IoT-scale products and the need for real-time insights. At Fuze, we have developed a backend platform as part of our mobility-oriented cloud service that uses Big Data-based approache...
May. 4, 2016 03:30 PM EDT Reads: 627
The increasing popularity of the Internet of Things necessitates that our physical and cognitive relationship with wearable technology will change rapidly in the near future. This advent means logging has become a thing of the past. Before, it was on us to track our own data, but now that data is automatically available. What does this mean for mHealth and the "connected" body? In her session at @ThingsExpo, Lisa Calkins, CEO and co-founder of Amadeus Consulting, will discuss the impact of wea...
May. 4, 2016 03:00 PM EDT Reads: 1,175
SYS-CON Events announced today that Ericsson has been named “Gold Sponsor” of SYS-CON's @ThingsExpo, which will take place on June 7-9, 2016, at the Javits Center in New York, New York. Ericsson is a world leader in the rapidly changing environment of communications technology – providing equipment, software and services to enable transformation through mobility. Some 40 percent of global mobile traffic runs through networks we have supplied. More than 1 billion subscribers around the world re...
May. 4, 2016 01:45 PM EDT Reads: 1,338
SYS-CON Events announced today that Peak 10, Inc., a national IT infrastructure and cloud services provider, will exhibit at SYS-CON's 18th International Cloud Expo®, which will take place on June 7-9, 2016, at the Javits Center in New York City, NY. Peak 10 provides reliable, tailored data center and network services, cloud and managed services. Its solutions are designed to scale and adapt to customers’ changing business needs, enabling them to lower costs, improve performance and focus inter...
May. 4, 2016 01:00 PM EDT Reads: 1,431
We're entering the post-smartphone era, where wearable gadgets from watches and fitness bands to glasses and health aids will power the next technological revolution. With mass adoption of wearable devices comes a new data ecosystem that must be protected. Wearables open new pathways that facilitate the tracking, sharing and storing of consumers’ personal health, location and daily activity data. Consumers have some idea of the data these devices capture, but most don’t realize how revealing and...
May. 4, 2016 12:45 PM EDT Reads: 624
The IoTs will challenge the status quo of how IT and development organizations operate. Or will it? Certainly the fog layer of IoT requires special insights about data ontology, security and transactional integrity. But the developmental challenges are the same: People, Process and Platform. In his session at @ThingsExpo, Craig Sproule, CEO of Metavine, will demonstrate how to move beyond today's coding paradigm and share the must-have mindsets for removing complexity from the development proc...
May. 4, 2016 12:45 PM EDT Reads: 920
trust and privacy in their ecosystem. Assurance and protection of device identity, secure data encryption and authentication are the key security challenges organizations are trying to address when integrating IoT devices. This holds true for IoT applications in a wide range of industries, for example, healthcare, consumer devices, and manufacturing. In his session at @ThingsExpo, Lancen LaChance, vice president of product management, IoT solutions at GlobalSign, will teach IoT developers how t...
May. 4, 2016 12:15 PM EDT Reads: 560
There is an ever-growing explosion of new devices that are connected to the Internet using “cloud” solutions. This rapid growth is creating a massive new demand for efficient access to data. And it’s not just about connecting to that data anymore. This new demand is bringing new issues and challenges and it is important for companies to scale for the coming growth. And with that scaling comes the need for greater security, gathering and data analysis, storage, connectivity and, of course, the...
May. 4, 2016 11:15 AM EDT Reads: 1,246
Increasing IoT connectivity is forcing enterprises to find elegant solutions to organize and visualize all incoming data from these connected devices with re-configurable dashboard widgets to effectively allow rapid decision-making for everything from immediate actions in tactical situations to strategic analysis and reporting. In his session at 18th Cloud Expo, Shikhir Singh, Senior Developer Relations Manager at Sencha, will discuss how to create HTML5 dashboards that interact with IoT devic...
May. 4, 2016 11:00 AM EDT Reads: 1,393
So, you bought into the current machine learning craze and went on to collect millions/billions of records from this promising new data source. Now, what do you do with them? Too often, the abundance of data quickly turns into an abundance of problems. How do you extract that "magic essence" from your data without falling into the common pitfalls? In her session at @ThingsExpo, Natalia Ponomareva, Software Engineer at Google, will provide tips on how to be successful in large scale machine lear...
May. 4, 2016 10:00 AM EDT Reads: 1,525
Digital payments using wearable devices such as smart watches, fitness trackers, and payment wristbands are an increasing area of focus for industry participants, and consumer acceptance from early trials and deployments has encouraged some of the biggest names in technology and banking to continue their push to drive growth in this nascent market. Wearable payment systems may utilize near field communication (NFC), radio frequency identification (RFID), or quick response (QR) codes and barcodes...
May. 4, 2016 08:15 AM EDT Reads: 1,016
You think you know what’s in your data. But do you? Most organizations are now aware of the business intelligence represented by their data. Data science stands to take this to a level you never thought of – literally. The techniques of data science, when used with the capabilities of Big Data technologies, can make connections you had not yet imagined, helping you discover new insights and ask new questions of your data. In his session at @ThingsExpo, Sarbjit Sarkaria, data science team lead ...
May. 4, 2016 08:00 AM EDT Reads: 1,204
The IETF draft standard for M2M certificates is a security solution specifically designed for the demanding needs of IoT/M2M applications. In his session at @ThingsExpo, Brian Romansky, VP of Strategic Technology at TrustPoint Innovation, will explain how M2M certificates can efficiently enable confidentiality, integrity, and authenticity on highly constrained devices.
May. 4, 2016 08:00 AM EDT Reads: 1,277
Manufacturers are embracing the Industrial Internet the same way consumers are leveraging Fitbits – to improve overall health and wellness. Both can provide consistent measurement, visibility, and suggest performance improvements customized to help reach goals. Fitbit users can view real-time data and make adjustments to increase their activity. In his session at @ThingsExpo, Mark Bernardo Professional Services Leader, Americas, at GE Digital, will discuss how leveraging the Industrial Interne...
May. 4, 2016 04:45 AM EDT Reads: 1,365
Whether your IoT service is connecting cars, homes, appliances, wearable, cameras or other devices, one question hangs in the balance – how do you actually make money from this service? The ability to turn your IoT service into profit requires the ability to create a monetization strategy that is flexible, scalable and working for you in real-time. It must be a transparent, smoothly implemented strategy that all stakeholders – from customers to the board – will be able to understand and comprehe...
May. 3, 2016 12:30 PM EDT Reads: 1,239
You deployed your app with the Bluemix PaaS and it's gaining some serious traction, so it's time to make some tweaks. Did you design your application in a way that it can scale in the cloud? Were you even thinking about the cloud when you built the app? If not, chances are your app is going to break. Check out this webcast to learn various techniques for designing applications that will scale successfully in Bluemix, for the confidence you need to take your apps to the next level and beyond.
May. 3, 2016 12:15 PM EDT Reads: 1,623