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

SELECT *
FROM art
ORDER BY ArtName

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



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 , , 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.

20 thoughts

  1. 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!

  2. 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.

  3. 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.

  4. 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

  5. @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=&qu

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

  7. 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.

  8. 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…..
    🙂

  9. 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

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

  11. 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.

Leave a Reply