Blog

20Jun
2009
Look, No Datasource

Look at this code snippet. Notice anything odd about it?

view plain print about
1<cfquery name="Art">
2SELECT *
3FROM art
4ORDER BY ArtName
5</cfquery>

Yep, the <CFQUERY> tag in the above snippet is missing the DATASOURCE attribute. And yet, this code works. Why? Take a look at this Application.cfc file:

view plain print about
1<cfcomponent>
2    <cfset this.datasource="cfartgallery">
3</cfcomponent>

In ColdFusion Centaur you can now specify an application wide default datasource by setting this.datasource in Application.cfc. The specified datasource becomes the default for all <CFQUERY>, <CFSTOREDPROC>, and other tags that expect a datasource to be specified. Of course, datasource can still be specified manually if needed, and a specified datasource will override the application default one.

This is one of those duh! enhancements that just makes sense. And, as I'll explain in an upcoming post, it also has another important purpose.

Related Blog Entries

Comments (20)



  • Freelance Web Development

    That's just one step ahead of a Coda clip I have...

    <cfquery name="read" dataSource="#application.datasource#">
    SELECT
    FROM
    WHERE
    </cfquery>

    That's a nice little enhancement. Thanks for sharing!

  • Russ S.

    Perfect! One less redundant thing to code.

    #2Posted by Russ S. | Jun 21, 2009, 02:46 AM
  • Barry

    Newbie question...can this work with Application.cfm?

    #3Posted by Barry | Jun 21, 2009, 05:30 AM
  • Ben Forta

    Barry, no, the application wide THIS settings (included those for custom tag path and mappings added in CF8) require Application.cfc.

    --- Ben

    #4Posted by Ben Forta | Jun 21, 2009, 07:53 AM
  • Raymond Camden

    @Barry: One more reason to stop using App.cfm. I only use App.cfm in my OS apps that have to support CFMX.

  • Sid Wing

    Yet another great enhancement in the Centaur release! Thanks for the blog on it!

    #6Posted by Sid Wing | Jun 21, 2009, 10:23 AM
  • Glyn Jackson

    It's nice that this can be set in the 'this' scope. now all we need is the same for cfmail.

  • Ulf Unger

    What with datasources that need user / password data to authenticate? Will this be supported in Application.cfc? I'm just thinking about shared hosting where you have to specify user / password with all cfquery tags.

    #8Posted by Ulf Unger | Jun 21, 2009, 01:57 PM
  • Ulf Unger

    What with datasources that need user / password data to authenticate? Will this be supported in Application.cfc? I'm just thinking about shared hosting where you have to specify user / password with all cfquery tags.

    #9Posted by Ulf Unger | Jun 21, 2009, 01:57 PM
  • greg h

    Wow!

    Well, bye bye to our ubiquitous, old reliable #application.dsn# ;-)

    I have been with CF since version 3. Man I could have used this ... last century!!!

    And there is was, hiding there in plain site (just waiting to be added as a feature in version 9).

    Better late than never. And again confirming exactly why CF is, bar none, the language with which I consistently remain my most productive in.

    Thanks!!!

    Best regards,

    g

    #10Posted by greg h | Jun 21, 2009, 05:41 PM
  • Raymond Camden

    @Ulf: While not quite as nice, don't forget you can pass structures to cf tags. This means you could create an application scoped structure with default mail params:

    <cfset s = {server="127.0.0.1",username="mail",password="pass"}>

    and then do

    <cfmail to="some@where.com" from="admin@foo.com" subject="Your Email" attributeCollection="#s#">

  • Jose

    What if you have more than one datasource in an application? How will the cfquery tag know which datasource to refer to?

    #12Posted by Jose | Jun 22, 2009, 09:32 AM
  • Hemant Khandelwal

    You can define only one datasource in Application.cfc. That would be the default one. If there is one in cfquery, the one defined in cfquery will be considered.

    #13Posted by Hemant Khandelwal | Jun 22, 2009, 12:06 PM
  • nchen

    Is this.datasource available in CF8?

    #14Posted by nchen | Jan 28, 2010, 02:36 PM
  • tam

    What would cause the error "Variable APPDSN is undefined" (where APPDSN is the name of the datasource variable). I have the following in my Application.cfc file:
    <cfset this.datasource = "APPDSN">
    Thanks for the useful info, I'm looking forward to using it once I get it to work.....
    :)

    #15Posted by tam | Feb 16, 2010, 09:22 AM
  • Ben Forta

    nchecn, no, this was added to CF9.

    tam, make sure you don't actually specify the datasource explicitly in your <cfquery> tags, remove the whole datasource="" attribute if you use this.datasource.

    --- Ben

    #16Posted by Ben Forta | Feb 16, 2010, 09:48 AM
  • cfkelvin

    I noticed in the cfdocs that a datasource is required for <cfstoredproc>. So this only works with <cfquery> or?

    #17Posted by cfkelvin | Nov 22, 2010, 08:41 AM
  • Houssem

    Hello,
    I tried using this.datasource in Application.cfc and it works in a local dev environment but in a remote server it displays :
    The value of the attribute datasource, which is currently '', is invalid.

    What could be the cause of this error please.

    Thanks,
    Houssem.

    #18Posted by Houssem | Dec 1, 2010, 08:07 AM
  • fabio

    how can i retrieve the current datasource previously set in that way? application.datasource?

    #19Posted by fabio | Jan 24, 2011, 10:36 AM
  • Adil

    I am getting this error:
    Attribute validation error for tag CFQUERY

    I am using Coldfusion9
    Please help

    #20Posted by Adil | Nov 24, 2011, 12:43 PM