A look into onApplicationStart (ColdFusion)

by kai on 20/01/2010

Well, most of you would know that the onApplicationStart() method in ColdFusion’s Application.cfc is a single-threaded method that’s being executed by ColdFusion after the start of the application. The reason for an application having the necessity to start could for instance be that CF or the underlying machine has been restarted or that the application had run into its timeout.

Recently while migrating a bunch of legacy application code from Application.cfm to Application.cfc, a team member of the team of my client asked me what happened at application start if there was an error (for example a typo in SQL code) in the onApplicationStart method.

The answer is that onApplicationStart will start to execute and (depending on your error handling strategy) just plainly fail with an error message. Your application will not be properly initialised though, i.e. ColdFusion would send the next incoming request to the onApplicationStart method again.

The ColdFusion documentation actually states the following: “If this method throws an uncaught exception or returns False, the application does not start and ColdFusion does not process any pages in the application. In this case, ColdFusion will run the onApplicationStart method the next time a user requests a page in the application.”

That’s actually mostly correct. Basically – if onApplicationStart() does NOT return true (when having specified a returntype) – every follow-up request will be run against onApplicationStart() again. However: from a technical point of view, your application is in some sort of a limbo state because it actually will exist (at least to a fragment) and application-scope variables could be created before the error occurs.

In reality the fact that application-scope variables and an application name will potentially exist even though the application hasn’t been initialized is not of a huge practical concern as the next request will hit onApplicationStart() again anyway, but it’s still an interesting thing to notice.

Ben Nadel January 24, 2010 at 12:44 pm

I have never found a great use for the boolean response to OnApplicationStart(). Typically, I leverage the OnRequestStart() boolean response (which works in a very similar way). I find this one is good for things like “Down for Maintenance” pages. But, I would be curious to see where / how OnApplicationStart() return values are put to good use.

Comments on this entry are closed.

Previous post:

Next post: