AdobeStock_455007340

Look, No Datasource

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 responses to “Look, No Datasource”

  1. Jules Gravinese Avatar
    Jules Gravinese

    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. Russ S. Avatar
    Russ S.

    Perfect! One less redundant thing to code.

  3. Barry Avatar
    Barry

    Newbie question…can this work with Application.cfm?

  4. Ben Forta Avatar
    Ben Forta

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

  5. Raymond Camden Avatar
    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.

  6. Sid Wing Avatar
    Sid Wing

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

  7. Glyn Jackson Avatar
    Glyn Jackson

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

  8. Ulf Unger Avatar
    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.

  9. Ulf Unger Avatar
    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.

  10. greg h Avatar
    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

  11. Raymond Camden Avatar
    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=&qu

  12. Jose Avatar
    Jose

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

  13. Hemant Khandelwal Avatar
    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.

  14. nchen Avatar
    nchen

    Is this.datasource available in CF8?

  15. tam Avatar
    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…..
    🙂

  16. Ben Forta Avatar
    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

  17. cfkelvin Avatar
    cfkelvin

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

  18. Houssem Avatar
    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.

  19. fabio Avatar
    fabio

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

  20. Adil Avatar
    Adil

    I am getting this error:
    Attribute validation error for tag CFQUERY
    I am using Coldfusion9
    Please help

Leave a Reply