Look at this code snippet. Notice anything odd about it?
SELECT *
FROM art
ORDER BY ArtName
Yep, the
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
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.
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!
Perfect! One less redundant thing to code.
Newbie question…can this work with Application.cfm?
Barry, no, the application wide THIS settings (included those for custom tag path and mappings added in CF8) require Application.cfc.
— Ben
@Barry: One more reason to stop using App.cfm. I only use App.cfm in my OS apps that have to support CFMX.
Yet another great enhancement in the Centaur release! Thanks for the blog on it!
It’s nice that this can be set in the ‘this’ scope. now all we need is the same for cfmail.
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.
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.
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
@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
What if you have more than one datasource in an application? How will the cfquery tag know which datasource to refer to?
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.
Is this.datasource available in CF8?
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…..
🙂
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
I noticed in the cfdocs that a datasource is required for <cfstoredproc>. So this only works with <cfquery> or?
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.
how can i retrieve the current datasource previously set in that way? application.datasource?
I am getting this error:
Attribute validation error for tag CFQUERY
I am using Coldfusion9
Please help