David Jacobson
David Jacobson

Reputation: 179

How do I reference a second DB from an application cfc file

In our Portal application.cfc, we are defining (setting up) our DSN connections like so:

<cfset this.datasource = "DSN1"> (Main DB) <cfset this.datasource_1 = "DSN2"> (2nd DB) <cfset this.datasource_2 = "DNS3"> (3rd DB)

These are in the tags. I have also moved then to the ‘OnApplicationStart’ function and cannot get it to work correctly.

This application.cfc if referenced in the main APP, that the other apps have access to (App2, etc…), my question is;

How do I reference the other datasources (this.datasource_2) in a query for the App2 application?

<cfquery name="queryname" datasource="**[What goes here]**"> For second datasource

The this.datasource DSN is always being referenced because there is no datasource listed in the cfquery tags.

Any help you can provide or links to send my way will be appreciated. Thanks in advance!

Upvotes: 2

Views: 183

Answers (3)

Notion
Notion

Reputation: 56

or you can use the Applicaton.cfc this.datasources struct

Upvotes: 0

Dan Bracuk
Dan Bracuk

Reputation: 20794

My solution is to not use any variables at all and simply hard code the datasource names. In other words, for this:

<cfquery name="queryname" datasource="**[What goes here]**"> For second datasource

The answer is "DSN2".

In order for these datasources to be usable, they have to be defined on the server and you have to know their names. If you assign them to some sort of global variable, then you would have to know the name of that variable. I see no value to using global variables in this situation.

For DSN1, there are pros and cons to using a global variable. The advantage of a variable is that you don't need a datasource attribute for the queries that use this database. The disadvantage is that your code becomes a little less consistent if some queries have a datasource attribute and others don't. That topic is best discussed in person while drinking beer.

Upvotes: 0

James A Mohler
James A Mohler

Reputation: 11120

Try seeing additional application variables

<cfset application.datasource_1 = "DSN2">  (2nd DB)
<cfset application.datasource_2 = "DNS3"> (3rd DB)

Upvotes: 2

Related Questions