Find the bug....

I was helping a user yesterday who ran into an odd problem. His CFC was using Application.DSN (which is another problem) and he kept getting an error that said Application.DSN didn’t exist. He copied his line that defined Application.DSN and set it in the Request scope. When he updated the CFC to use the Request scope, it worked fine. What was the problem? Here is some code that simulates his Application.cfm. See if you can spot it before reading on….

Application.cfm: <cfset application.dsn = "goo"> <cfset request.dsn = "goo"> <cfapplication name="myapp"> <cfset application.random = "Paris Hilton is my idol....">

test.cfm: <cfdump var=”#application#”> <cfdump var=”#request#”> </code> If you run this test, you will clearly see that the Application scope only has one variable, random. The Request scope shows the DSN. So now you can see what he saw - that the Application scope was working incorrectly and the Request scope worked fine.

But… look again. Do you see his CFAPPLICATION tag? It is after his application.dsn line. ColdFusion had not “started” the Application yet. Therefore his DSN variable was actually added to the magical unnamed Application scope area. To see what I mean, simply add a dump before his CFAPPLICATION tag.

So two lessons from this:

1) First, always remember to put the CFAPPLICATION tag before any code that actually uses the Application scope.

2) Second, this is one more big reason to migrate to Application.cfc. If he had used onApplicationStart(), this would not/could not have happened.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Comments