Adobe ColdFusion 8

Managing the application with Application.cfc

You use the onApplicationStart and onApplicationEnd methods to configure and manage the application; that is, to control resources that are used by multiple pages and requests and must be consistently available to all code in your application. Such resources can include data sources, application counters such as page hit variables, or style information for all pages.

The onApplicationStart method executes when ColdFusion gets the first request for a page in the application after the server starts. The onApplicationEnd method executes when the application server shuts down or if the application is inactive for the application time-out period.

The following sections describe some of the ways you can use these methods. For more information, see entries for onApplicationStart and onApplicationEnd in the CFML Reference.

Defining application utility functions

Functions that you define in Application.cfc and do not put in a shared scope are, by default, available only to other methods in the CFC.

If your Application.cfc implements the onRequest method, any utility functions that you define in Application.cfc are also directly available in to the target page, because Application.cfc and the target page share the Variables scope.

If your application requires utility functions that are used by multiple pages, not just by the methods in Application.cfc, and you do not use an onRequest method, Adobe recommends that you put them in a separate CFC and access them by invoking that CFC. As with other ColdFusion pages, Application.cfc can access any CFC in a directory path that is configured on the ColdFusion Administrator Mappings page. You can, therefore, use this technique to share utility functions across applications.

If your Application.cfc defines utility functions that you want available on request pages and does not use an onRequest method, you must explicitly put the functions in a ColdFusion scope, such as the Request scope, as the following code shows:

<cffunction name="theFunctionName" returntype="theReturnType">
    <!--- Function definition goes here. --->
</cffunction>

<cffunction name="OnRequestStart">
    <!--- OnRequestStart body goes here --->
        <cfset Request.theFunctionName=This.theFunctionName>
</cffunction>

On the request page, you would include the following code:

<cfset myVar=Request.theFunctionName(Argument1...)>

Functions that you define in this manner share the This scope and Variables scope with the Application.cfc file for the request.

Setting application default variables and constants in onApplicationStart

You can set default variables and application-level constants in Application.cfc. For example, you can do the following:

  • Specify a data source and ensure that it is available
  • Specify domain name
  • Set styles, such as fonts or colors
  • Set other application-level variables

You do not have to lock Application scope variables when you set them in the Application.cfc onApplicationStart method.

For details on implementing the onApplicationStart method, see onApplicationStart in the CFML Reference.

Using the onApplicationEnd method

Use the onApplicationEnd method for any clean-up activities that your application requires when it shuts down, such as saving data in memory to a database, or to log the application end. You cannot use this method to display data on a user page, because it is not associated with any request. The application ends, even if this method throws an exception. An application that is used often is unlikely to execute this method, except when the server is shut down.

For details on implementing the onApplicationEnd method, see onApplicationEnd in the CFML Reference.