When building an application that uses an Application.cfc file, many times the OnApplicationStart() method is used to initialize Application scope variables.

Since this method is only called once until the next server restart, its safe to set your Application variables here without using cflock tags. A common implementation may look like this:

<cffunction name="onApplicationStart" returntype="boolean">
	<cfset Application.dsn = "myDSN">
	<cfset Application.smtpserver = "localhost">
</cffunction>

But what happens when you need to change one of those variables? For example if you change the Application.smtpserver value to “newhost.com”, and save the file, Application.smtpserver will still be set to localhost. Restarting the ColdFusion server will force the OnApplicationStart() method to be called again, thus loading in your new value, but that is a little drastic.

I’ve seen some developer’s change the this.name variable in Application.cfc – say from myapp-1.1.1 to myapp-1.1.2. This will cause ColdFusion to see the codebase as a new “application”, and thus call the OnApplicationStart() method again. The downside to doing it this way is that your sessions are tied to your application name, so they all get reset – forcing all your users to log in again, or perhaps canceling that shopping cart they were loading up. It also leaves the old application in memory until it reaches your this.applicationtimeout value.

Another way to reload the Application scope is to tell your application to manually call the OnApplicationStart() method. For example, in the OnRequestStart() method, add something like:

<cfif IsDefined("URL.refresh") AND URL.refresh EQ "yes">
 	<cfset OnApplicationStart()>
</cfif>

To be safe you should add <cflock> tags around everything in your OnApplicationStart() method, as you can no longer guarantee it will be run single-threaded. It would also be a good idea to modify that cfif statement to make sure the request was coming from an IP on your local network, or perhaps that an additional security key was passed in.

Now, to get to the point of this long-winded post. I’m always a little skeptical if the method was actually called. Was I supposed to add refresh=true, or reload=yes, etc.? On a few of our sites, the default page is actually an html file, so if I put this in my browser – http://mysite.com/?reload=yes, it wouldn’t even reach ColdFusion. So some feedback would be nice. What if we just wrote out a little message that the method had been called?

<cfif IsDefined("URL.refresh") AND URL.refresh EQ "yes">
 	<cfset OnApplicationStart()>
 	<p>The Application scope was reloaded.</p>
</cfif>

The problem with this is that you’d be outputting html before your DOCTYPE tag was even served, which is not a good thing. Also depending on how your site is setup with css, you may not even see that message. Luckily there is a rarely used ColdFusion tag that is helpful here. CFHTMLHEAD takes one argument – text which it will then place into your document, between your <head> tags. Beautiful! But we wouldn’t want to be displaying content inside our head tags. But its a great place to place some JavaScript.

<cfif IsDefined("URL.refresh") AND URL.refresh EQ "yes">
 	<cfset OnApplicationStart()>
 	<cfhtmlhead text="<script language=""JavaScript"">alert('Application was refreshed.');</script>">
</cfif>

Now when I tell my app to reload the Application scope, I get nice little popup confirmation message that it has actually taken place. You could even get fancy and display what the current variables/values are in your JavaScript popup.

9 Comments

  1. Ben Nadel says:

    I have to say, this is the first time I have ever seen a use of CFHtmlHead that I actually agree with. Sweeet tip!

  2. Dan Vega says:

    I have to agree with Ben. This is the first time I have actually saw an example of cfhtmlhead used in a position that actually makes sense. I used similar techniques for refreshing the application scope but great tip!

  3. Axel says:

    Thats hottt…

  4. Jules says:

    This is a great tip.
    I am reasonably new to CF, I am using CF8 for a couple weeks, after coming from an MS environment – learning how CF deals with common scenarios is great.

    This is a great tip and it is good to learn that the Macromendia/Adobe guys have got the logic of this kind of HTML Head management sorted for the lifetime of the request.

    I would like to add that in order to deliver absolute XHTML and standards compliance in a consistently reliable manner when you are dealing with dynamic pages (I mostly develop web applications as opposed to sites) is to use cfhtmlhead for ALL clients-script activity – instead of just outputing some script as required and have it appear in the repsonse where you made the output, just simply wrap it in this construct and hey presto – the script is where it should be. Lovely.

    Excellent tip – highly recommendable – this is a massive issue for me when dealing with dev, test, stage and live – the ability to reload the application scope in ASP.NET is brilliant – CF lacks this, but this tip makes up for it.

    Cheers

  5. jyoseph says:

    Incredibly simple and straight forward instruction on how to reset the application w/ out resetting coldfusion, thank you so much!

  6. cep parça kontör says:

    online parça kontör tam kontör yükleme merkezi

  7. taksator says:

    thanks

  8. olcay says:

    very good work man thanks

  9. darsane says:

    Thanks for the information my friend