2

When is it appropriate to place DSN definitions inside the onApplicationStart() function vs outside of the function?

I have seen this method:

<cfset this.datasource = "datasource_name">
  <cffunction name="onApplicationStart" returnType="boolean" output="false">

and I've seen it this way...

<cffunction name="onApplicationStart" returnType="boolean" output="false">
  <cfset application.dsn = "datasource_name">

The only reasons I can find for this format is leaving the datasource outside the function is an older, still supported, but outdated way of doing things.

Is there any other reason for it?

I did find this question already, but it only states where DSNs should be declared, but not when to it should go inside vs outside.

James A Mohler
  • 11,060
  • 15
  • 46
  • 72
Millhorn
  • 2,953
  • 7
  • 39
  • 77
  • 6
    If I am not mistaken, if you do `this.datasource` then it is an application wide default. `` does not even need datasource specified.(a good thing). If you set an application wide variable, you have to specify the datasource in each and every ``, (a bad thing) – James A Mohler Jun 11 '20 at 19:01
  • 3
    ... and in case it is not clear, the `this.datasource` syntax/property is actually newer than the old concept of using an `application.dsn` variable – SOS Jun 11 '20 at 20:47
  • I figured this might come down to just refactoring my application to account for CF2018. Thanks! – Millhorn Jun 11 '20 at 21:32
  • 2
    It should be a simple find and replace with nothing. I sometimes say, the code that does not exist, you do not maintain. – James A Mohler Jun 12 '20 at 01:26

1 Answers1

2

Q: Should I place a DSN (datasource) definition inside or outside onApplicationStart() function?

A: Outside

That way <cfquery>, QueryExecute(), and ORM can tap into that datasource information without that information being repeated.

James A Mohler
  • 11,060
  • 15
  • 46
  • 72