Default Application Datasources in ColdFusion
By Pete Freitag
It would be handy if you could specify a default datasource name in the cfapplication
tag, or Application.cfc, and then omit the datasource
attribute in the cfquery
tag.
I don't know about you but most apps I've built only have one datasource, and I typically set the datasource name with a variable in the Application.cfm file. Being able to omit the datasource
attribute would save a bunch of typing.
I just suggested this feature on the Macromedia wish form.
Update - this feature has been added to ColdFusion 9!
As of ColdFusion 9 you can add this.datasource
to your Application.cfc
file, and set it to the name of the default datasource. Once you have done this, you no longer need to specify the datasource attribute on the cfquery tag!
component { this.name = "myApp"; this.datasource = "myDatasourceName"; }
Then you can omit the datasource attribute on the cfquery tag, like this:
<cfquery name="news"> SELECT id, headline, story FROM news </cfquery>
Or using queryExecute
:
news = queryExecute("SELECT id, headline, story FROM news");
And finally if you are using an Application.cfm
file instead of an Application.cfc
file, you can specify the default datasource in the cfapplication
tag:
<cfapplication name="myApp" datasource="myDatasourceName">
Default Application Datasources in ColdFusion was first published on August 11, 2005.
The FuseGuard Web Application Firewall for ColdFusion & CFML is a high performance, customizable engine that blocks various attacks against your ColdFusion applications.
CFBreak
The weekly newsletter for the CFML Community
Comments
Something like:
<pre>
<cfif thisTag.ExecutionMode is "start">
<cfparam name="attributes.name" type="string" />
<cfparam name="attributes.datasource" type="string" default="myDatasourceName">
<cfelseif thisTag.ExecutionMode is "end">
<cfquery name="query">
#thisTag.generatedContent#
</cfquery>
<cfset thisTag.GeneratedContent = "" />
<cfset caller[attributes.name] = query />
</cfif>
</pre>
Of course, you might have to write an additional custom tag to do what cfqueryparam does.
If you only ever use one DSN, just write one method that queries a DSN and pass SQL (and any other params you want to set) to it? Seems like a problem which could be solved by trying something different with the design of said app?
Context gives your application Persist Scope, meaning your Application can one day use XML as its storage, then the next it could use SQL.
What happens if you store information within Multiple DSN's? Audit Information goes in DSNA, while mainstream Data goes into DSNB (why? seperation of security for one).
I've built / used a config business object, and I pass that into all of my "Services/Managers/Herders/Builder" classes. This configuration object initializes itself based on an XML packet that I "couple" with the application. Now, at this point i don't simply have routines "getDSN()" for example.
I class my overall DSN information based on the persist service they provide, meaning i have a method within my config which is like: "getPersistService('security')" which depending on the DB Server type, returns appropriate DSN information.
Another classic mistake is folks sometimes use username + password, while other times they don't (ie shared hosting, its a must. Dedicated hosting, not so much).
Point is this, the only points of customization within my cf applications are:
- config.xml
- DAO_SQL, DAO_Oracle, DG_SQL, DG_Oracle etc..
The rest of the business logic doesn't give two hoots. The main reason for this is simply that while Application.cfc is great, its easily abused and in a nutshell Application.cfm is probably the best suited aspect of a coldfusion application which provides appropriate context to its intended use, resulting in promoting a better "re-use" capability within your code.
If i had to pick this setting, i'd much prefer Barneys approach whereby we leveridge some kind of Mapping/Alias to suite our CFQUERY code. To me thats a much easier and more elegant approach.
We've been looking for potential database load balancing software as well, and came across this: http://c-jdbc.objectweb.org/
Nice thing about CJDBC is that it's a multiplexing datasource, so in theory, you get load balancing without modifying your application code.
Note that I've not been able to look at this yet, but this is one of the ideas that's on the cards to investigate as a matter of performance improvements.
http://www.bennadel.com/blog/1642-Learning-ColdFusion-9-Application-Specific-Data-Sources.htm
In the meantime, just set up a snippet in CFE or DW that does the typing for you. Same for CFQUERYPARAM.