Learning ColdFusion 9: Application-Specific Data Sources

Posted July 13, 2009 at 10:27 AM

Tags: ColdFusion

As I stated in my blog post on ColdFusion 9 implicit struct and array usage, I think that it's sometimes the little feature upgrades that make the biggest difference in the long run. Another little feature in ColdFusion 9 that's going to make our lives a lot easier is the addition of application-specific data sources. While this application property is oddly missing from the beta documentation, if you've followed ColdFusion blogs, you've probably seen that in ColdFusion 9's Application.cfc, you can now define your query datasource as an attribute of the THIS scope:

 Launch code in new window » Download code as text file »

  • <cfcomponent
  • output="false"
  • hint="I define application settings and event handlers.">
  •  
  • <!--- Define application settings. --->
  • <cfset this.name = hash( getCurrentTemplatePath() ) />
  •  
  • <!---
  • Define the data source to be used by all CFQuery tags
  • within this application. With this in place, you will not
  • need to define a Datasource attribute in and of your
  • CFQuery tags.
  • --->
  • <cfset this.datasource = "ben" />
  •  
  • <!--- Define page settings. --->
  • <cfsetting showdebugoutput="false" />
  •  
  • </cfcomponent>

Notice that Application.cfc now has a "this.datasource" property. With this defined, I no longer need to include a Datasource attribute in any of my CFQuery tags. To test this, I set up a simple index.cfm page that creates, populates, and then queries a table in the embedded Apache Derby database:

 Launch code in new window » Download code as text file »

  • <!--- Drop the exist table. --->
  • <cfquery name="drop">
  • DROP TABLE girl
  • </cfquery>
  •  
  •  
  • <!---
  • Create the girls data table. The database I'm using
  • is an embeded Apach Derby database, hence the really
  • odd auto-increment syntax.
  • --->
  • <cfquery name="create">
  • CREATE TABLE girl
  • (
  • id int NOT NULL GENERATED BY DEFAULT AS IDENTITY,
  • name varchar(30) NOT NULL,
  • hair varchar(30) NOT NULL,
  • PRIMARY KEY (id)
  • )
  • </cfquery>
  •  
  •  
  • <!--- Insert a records into the girl. --->
  • <cfquery name="insert">
  • INSERT INTO girl
  • (
  • name,
  • hair
  • ) VALUES (
  • <cfqueryparam value="Tricia" cfsqltype="cf_sql_varchar" />,
  • <cfqueryparam value="Brown" cfsqltype="cf_sql_varchar" />
  • )
  • </cfquery>
  •  
  •  
  • <!--- Query for girls. --->
  • <cfquery name="girls">
  • SELECT
  • id,
  • name,
  • hair
  • FROM
  • girl
  • </cfquery>
  •  
  •  
  • <!--- Output query records. --->
  • <cfdump
  • var="#girls#"
  • label="Girls (Derby)"
  • />

Notice that none of the CFQuery tags on the page define anything more than the Name attribute. The Datasource value which we would normally need now feeds of the application-specific Datasource property in the Application.cfc. When we run the above code, we get the following CFDump output:

 
 
 
 
 
 
ColdFusion 9 Application-Specific Datasource Property Allows Global Datasource Definition For Every CFQuery Tag. 
 
 
 

It works perfectly.

Just like most other application properties, this value can be changed on a per-page-request basis. Meaning, if you had this code in your Application.cfc:

 Launch code in new window » Download code as text file »

  • <!--- Randomly set the app-specific data source. --->
  • <cfif (randRange( 1, 2 ) EQ 1)>
  • <cfset this.datasource = "ben" />
  • <cfelse>
  • <cfset this.datasource = "ben_bunk" />
  • </cfif>

... half of your page requests would error out. This, of course, is a silly example; but, if you needed to, you could easily extend your root Application.cfc and override the Datasource property. Keep in mind that this datasource property is a property of the application and not of any specific CFQuery tag. This means that if you create a CFC with one datasource and cache it, changing the datasource in the Application.cfc post-caching will still trickle down into the cached CFC, causing errors.

In the past, ColdFusion has been quite lenient for those of you who refuse to take up Application.CFC and abandon Application.CFM. Well, the "Datasource" property is not available on the CFApplication tag. If you want to use this (and other application-specific properties such as Mappings and CustomTagPaths) you need to be using Application.cfc. And, once you do, I think you'll find that this small upgrade will make life a whole lot nicer.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Other Searches  |  Print Page


You Might Also Be Interested In:



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Jul 13, 2009 at 1:21 PM // reply »
4 Comments

And so is the cool ORM integration, which wont be available to Application.cfm users


Jul 13, 2009 at 1:25 PM // reply »
7,572 Comments

@Rahul,

Yeah... people seriously need to start using Application.cfc. It really just offers so many more advanced features!


Jul 13, 2009 at 7:34 PM // reply »
21 Comments

A nice feature, making CF just a little more convienient.


Jul 13, 2009 at 7:35 PM // reply »
21 Comments

Oh, and how do I get an avatar for my comments?? :)


Jul 13, 2009 at 8:00 PM // reply »
51 Comments

The change from application.cfm to application.cfc is going to be that much more easier with the extra functionality added to cfscript.

I come from a PHP background and believe that while markup has it's places (ie: cfquery), scripting is much easier to read and code.

If only we could drop in and out of cfscript while in the middle of loops and similar.

P.S: I have also been searching on how to change my avatar too.


Jul 14, 2009 at 12:26 AM // reply »
10 Comments

Hey Ben, Its Cool! What if we would like to go for extra attributes like username/password. can we also describe the same in the (this) scope in application.cfc to make it easier for a bit of security or not


Jul 14, 2009 at 12:30 AM // reply »
51 Comments

@Misty

Wouldn't you just set the other attributes like username and password for the datasource in CF administration?


Jul 14, 2009 at 12:57 AM // reply »
40 Comments

@Adam

Go to http://en.gravatar.com/ and sign up. A ton of sites out there use them. Then whenever you post to a site that uses them (and use the same e-mail), it will show up. :)


Jul 14, 2009 at 1:06 AM // reply »
21 Comments

Ah, got it. I had a Gravatar account, but under an older email address. This should work much better.


Jul 14, 2009 at 1:06 AM // reply »
21 Comments

Bah!!! Hmm, oh well.


Jul 18, 2009 at 4:03 PM // reply »
7,572 Comments

@Misty,

From what I can tell, there are no username / password attributes for the data source. Like Andrew is saying, you just need to set that up in the CFAdmin and refer only to the data source in the code.

As someone who is used to used passing username and password via the CFQuery tag traditionally, this will actually be a nice "forced" change :)


Post Comment  |  Ask Ben

Recent Blog Comments
Mar 20, 2010 at 12:07 PM
Drawing On The iPhone Canvas With jQuery And ColdFusion
Simply awesome. Saved my day. ... read »
Mar 20, 2010 at 9:00 AM
Building A Fixed-Position Bottom Menu Bar (ala FaceBook)
I would like to say thx for an easy way to create a bottom bar. I do have a ?. Is it possible to center the bar if i want to resize it to ex 85%. Regards Offenbach ... read »
Mar 19, 2010 at 7:26 PM
MySQL 3/4 - com.mysql.jdbc.Driver And allowMultiQueries=true
Thank you very much for this post. Adding allowMultiQueries="true" in context.xml didn't help until I added it to url as allowMultiQueries=true Good idea is to use prepared statements and it will he ... read »
Jim
Mar 19, 2010 at 4:49 PM
Nobody Puts Baby In The Corner!
Wow. This is like suddenly finding a support group for your secret shame. I'm not alone! I always liked this movie, even though it is extremely cheesy. I just wish Jennifer Grey hadn't gotten the ... read »
Mar 19, 2010 at 4:47 PM
Application.cfc OnRequest() Method Affects OnError() Arguments
@Jason and @Ben, I've been doing some CF9 refactoring on our systems and noticed an odd occurrence with onError as well. Found a way to work around my problem, but what I saw was... Background: Our ... read »
Jim
Mar 19, 2010 at 4:44 PM
Shoot 'Em Up Starring Clive Owen And Paul Giamatti
I actually enjoyed this movie quite a lot. It was different, certainly, but I think they were going for more of a Quentin Tarentino-"wow, that was weird"-vibe than an actual spoof. Once I realize ... read »
Mar 19, 2010 at 4:34 PM
An Intensive Exploration Of jQuery With Ben Nadel (Video Presentation)
Hey I guess the video is down. Is there anyway you can upload to youtube or vimeo or some other service? Greatly appreciated. ... read »
Mar 19, 2010 at 4:24 PM
ColdFusion CFPOP - My First Look
@Ben Thanks for the follow up! The root of the problem had to do with being able to trace bounced emails to specific records in a DB table. Let's say you run an email campaign and you get 1,000 bou ... read »