define global DSN(s) in Coldfusion 10

| | August 6, 2015

I’ve been building a new coldfusion application leveraging object oriented approach and stored procedures. While everything works like a charm, the app is very fast due to SPs and optimized mysql code, I would really appreciate if you would help me clear one thing up :) I have dbcode.cfc which as you probably already guessed stores all queries with stored procedures. That said, generally what’s the best approach when it comes to storing global DNS parameters?

1.this can be used for one global DSN

<cfset this.datasource ="myDB">

2.this can also be used for one global DSN

<cffunction name="onApplicationStart" returnType="boolean" output="false">
<cfset application.dsn = "myDB">
<cfset application.username = "userName">
<cfset application.password = "password">
<cfreturn true>
</cffunction>

2.1 everything as above but onRequest

3.in my case I could also create a global variables within the dbcode.cfc

<cfset variables.dsn = "myDB">
<cfset variables.username = "userName">
<cfset variables.password = "password">

4.additionally one could use something like this for setting multiple datasources

<cffunction name="onApplicationStart" returnType="boolean" output="false">
<cfset application.myDSNs = StructNew()>
<cfset application.myDSNs.1 = "myDB1">
<cfset application.myDSNs.2 = "myDB2">
.
.
.
<!---something similar for usernames and passwords--->
</cffunction>

so what’s the most efficient way to handle this sort of thing?

One Response to “define global DSN(s) in Coldfusion 10”

  1. James A Mohler on November 30, -0001 @ 12:00 AM

    Let me see if I can answer each part of your DSN questions.

    1. Setting the datasource as a part the application.cfc constructor is OK

    2. Setting up the DSN in OnApplicationStart() works too, but you need to use:

      <cfset application.datasource = "myDB">
      

    2.1 Setting the datasource on every request is pointless, unless you datasource changes with every request. I suspect is does not

    1. You should not touch queries.cfc. That is internal to ColdFusion and has the potential to break everything on your server.

    2. You may want to read on up on the difference between arrays and structs. It looks like what you wanted to do was:

    <cfset application.myDSNs = ["MyDB1", "MyDB2]>

    They would then be accessable via:

    #application.myDSNs[1]#
    #application.myDSNs[2]#
    

    I would not do this approach. Databases are typically not arrays of anything. Each server has its own purpose. You DSNs should be reflective of one of more of the following:

    • The Server
    • The Database
    • Purpose

Leave a Reply