|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.
SOA Software has changed its name to Akana. With roots in Web Services and SOA Governance, Akana has established itself as a leader in API Management and is expanding into cloud integration as an alternative to the traditional heavyweight enterprise service bus (ESB). The company recently announced that it achieved more than 90% year-over-year growth. As Akana, the company now addresses the evolution and diversification of SOA, unifying security, management, and DevOps across SOA, APIs, microservices, and more.
Mar. 30, 2015 08:30 AM EDT Reads: 2,064
GENBAND has announced that SageNet is leveraging the Nuvia platform to deliver Unified Communications as a Service (UCaaS) to its large base of retail and enterprise customers. Nuvia’s cloud-based solution provides SageNet’s customers with a full suite of business communications and collaboration tools. Two large national SageNet retail customers have recently signed up to deploy the Nuvia platform and the company will continue to sell the service to new and existing customers. Nuvia’s capabilities include HD voice, video, multimedia messaging, mobility, conferencing, Web collaboration, deskt...
Mar. 30, 2015 01:00 AM EDT Reads: 1,484
SYS-CON Media announced today that @WebRTCSummit Blog, the largest WebRTC resource in the world, has been launched. @WebRTCSummit Blog offers top articles, news stories, and blog posts from the world's well-known experts and guarantees better exposure for its authors than any other publication. @WebRTCSummit Blog can be bookmarked ▸ Here @WebRTCSummit conference site can be bookmarked ▸ Here
Mar. 29, 2015 10:00 PM EDT Reads: 1,823
SYS-CON Events announced today that Cisco, the worldwide leader in IT that transforms how people connect, communicate and collaborate, has been named “Gold Sponsor” of SYS-CON's 16th International Cloud Expo®, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. Cisco makes amazing things happen by connecting the unconnected. Cisco has shaped the future of the Internet by becoming the worldwide leader in transforming how people connect, communicate and collaborate. Cisco and our partners are building the platform for the Internet of Everything by connecting the...
Mar. 29, 2015 07:00 PM EDT Reads: 5,255
SYS-CON Events announced today that robomq.io will exhibit at SYS-CON's @ThingsExpo, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. robomq.io is an interoperable and composable platform that connects any device to any application. It helps systems integrators and the solution providers build new and innovative products and service for industries requiring monitoring or intelligence from devices and sensors.
Mar. 29, 2015 06:00 PM EDT Reads: 1,496
The WebRTC Summit 2014 New York, to be held June 9-11, 2015, at the Javits Center in New York, NY, announces that its Call for Papers is open. Topics include all aspects of improving IT delivery by eliminating waste through automated business models leveraging cloud technologies. WebRTC Summit is co-located with 16th International Cloud Expo, @ThingsExpo, Big Data Expo, and DevOps Summit.
Mar. 29, 2015 06:00 PM EDT Reads: 1,616
Temasys has announced senior management additions to its team. Joining are David Holloway as Vice President of Commercial and Nadine Yap as Vice President of Product. Over the past 12 months Temasys has doubled in size as it adds new customers and expands the development of its Skylink platform. Skylink leads the charge to move WebRTC, traditionally seen as a desktop, browser based technology, to become a ubiquitous web communications technology on web and mobile, as well as Internet of Things compatible devices.
Mar. 29, 2015 06:00 PM EDT Reads: 1,867
Docker is an excellent platform for organizations interested in running microservices. It offers portability and consistency between development and production environments, quick provisioning times, and a simple way to isolate services. In his session at DevOps Summit at 16th Cloud Expo, Shannon Williams, co-founder of Rancher Labs, will walk through these and other benefits of using Docker to run microservices, and provide an overview of RancherOS, a minimalist distribution of Linux designed expressly to run Docker. He will also discuss Rancher, an orchestration and service discovery platf...
Mar. 29, 2015 04:15 PM EDT Reads: 2,447
Wearable technology was dominant at this year’s International Consumer Electronics Show (CES) , and MWC was no exception to this trend. New versions of favorites, such as the Samsung Gear (three new products were released: the Gear 2, the Gear 2 Neo and the Gear Fit), shared the limelight with new wearables like Pebble Time Steel (the new premium version of the company’s previously released smartwatch) and the LG Watch Urbane. The most dramatic difference at MWC was an emphasis on presenting wearables as fashion accessories and moving away from the original clunky technology associated with t...
Mar. 29, 2015 04:00 PM EDT Reads: 1,439
SYS-CON Events announced today that Vitria Technology, Inc. will exhibit at SYS-CON’s @ThingsExpo, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. Vitria will showcase the company’s new IoT Analytics Platform through live demonstrations at booth #330. Vitria’s IoT Analytics Platform, fully integrated and powered by an operational intelligence engine, enables customers to rapidly build and operationalize advanced analytics to deliver timely business outcomes for use cases across the industrial, enterprise, and consumer segments.
Mar. 29, 2015 03:30 PM EDT Reads: 2,180
SYS-CON Events announced today that Solgenia will exhibit at SYS-CON's 16th International Cloud Expo®, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY, and the 17th International Cloud Expo®, which will take place on November 3–5, 2015, at the Santa Clara Convention Center in Santa Clara, CA. Solgenia is the global market leader in Cloud Collaboration and Cloud Infrastructure software solutions. Designed to “Bridge the Gap” between Personal and Professional Social, Mobile and Cloud user experiences, our solutions help large and medium-sized organizations dr...
Mar. 29, 2015 03:00 PM EDT Reads: 2,873
SYS-CON Events announced today that Liaison Technologies, a leading provider of data management and integration cloud services and solutions, has been named "Silver Sponsor" of SYS-CON's 16th International Cloud Expo®, which will take place on June 9-11, 2015, at the Javits Center in New York, NY. Liaison Technologies is a recognized market leader in providing cloud-enabled data integration and data management solutions to break down complex information barriers, enabling enterprises to make smarter decisions, faster.
Mar. 29, 2015 03:00 PM EDT Reads: 3,485
@ThingsExpo has been named the Top 5 Most Influential M2M Brand by Onalytica in the ‘Machine to Machine: Top 100 Influencers and Brands.' Onalytica analyzed the online debate on M2M by looking at over 85,000 tweets to provide the most influential individuals and brands that drive the discussion. According to Onalytica the "analysis showed a very engaged community with a lot of interactive tweets. The M2M discussion seems to be more fragmented and driven by some of the major brands present in the M2M space. This really allows some room for influential individuals to create more high value inter...
Mar. 29, 2015 01:45 PM EDT Reads: 4,678
The world's leading Cloud event, Cloud Expo has launched Microservices Journal on the SYS-CON.com portal, featuring over 19,000 original articles, news stories, features, and blog entries. DevOps Journal is focused on this critical enterprise IT topic in the world of cloud computing. Microservices Journal offers top articles, news stories, and blog posts from the world's well-known experts and guarantees better exposure for its authors than any other publication. Follow new article posts on Twitter at @MicroservicesE
Mar. 29, 2015 12:00 PM EDT Reads: 1,468
After making a doctor’s appointment via your mobile device, you receive a calendar invite. The day of your appointment, you get a reminder with the doctor’s location and contact information. As you enter the doctor’s exam room, the medical team is equipped with the latest tablet containing your medical history – he or she makes real time updates to your medical file. At the end of your visit, you receive an electronic prescription to your preferred pharmacy and can schedule your next appointment.
Mar. 29, 2015 12:00 PM EDT Reads: 835
The list of ‘new paradigm’ technologies that now surrounds us appears to be at an all time high. From cloud computing and Big Data analytics to Bring Your Own Device (BYOD) and the Internet of Things (IoT), today we have to deal with what the industry likes to call ‘paradigm shifts’ at every level of IT. This is disruption; of course, we understand that – change is almost always disruptive.
Mar. 29, 2015 11:45 AM EDT Reads: 1,142
SYS-CON Events announced today that SafeLogic has been named “Bag Sponsor” of SYS-CON's 16th International Cloud Expo® New York, which will take place June 9-11, 2015, at the Javits Center in New York City, NY. SafeLogic provides security products for applications in mobile and server/appliance environments. SafeLogic’s flagship product CryptoComply is a FIPS 140-2 validated cryptographic engine designed to secure data on servers, workstations, appliances, mobile devices, and in the Cloud.
Mar. 29, 2015 11:00 AM EDT Reads: 1,436
SYS-CON Events announced today the IoT Bootcamp – Jumpstart Your IoT Strategy, being held June 9–10, 2015, in conjunction with 16th Cloud Expo and Internet of @ThingsExpo at the Javits Center in New York City. This is your chance to jumpstart your IoT strategy. Combined with real-world scenarios and use cases, the IoT Bootcamp is not just based on presentations but includes hands-on demos and walkthroughs. We will introduce you to a variety of Do-It-Yourself IoT platforms including Arduino, Raspberry Pi, BeagleBone, Spark and Intel Edison. You will also get an overview of cloud technologies s...
Mar. 29, 2015 11:00 AM EDT Reads: 2,121
SYS-CON Events announced today that Akana, formerly SOA Software, has been named “Bronze Sponsor” of SYS-CON's 16th International Cloud Expo® New York, which will take place June 9-11, 2015, at the Javits Center in New York City, NY. Akana’s comprehensive suite of API Management, API Security, Integrated SOA Governance, and Cloud Integration solutions helps businesses accelerate digital transformation by securely extending their reach across multiple channels – mobile, cloud and Internet of Things. Akana enables enterprises to share data as APIs, connect and integrate applications, drive part...
Mar. 28, 2015 04:15 PM EDT Reads: 1,565
Cloud is not a commodity. And no matter what you call it, computing doesn’t come out of the sky. It comes from physical hardware inside brick and mortar facilities connected by hundreds of miles of networking cable. And no two clouds are built the same way. SoftLayer gives you the highest performing cloud infrastructure available. One platform that takes data centers around the world that are full of the widest range of cloud computing options, and then integrates and automates everything. Join SoftLayer on June 9 at 16th Cloud Expo to learn about IBM Cloud's SoftLayer platform, explore se...
Mar. 28, 2015 02:00 PM EDT Reads: 1,650