User Guide

Table Of Contents
286 Chapter 13: Designing and Optimizing a ColdFusion Application
When ColdFusion MX receives a request, it instantiates the Application CFC and runs the
Application.cfc code in the following order:
CFC initialization code at the top of the file
onApplicationStart, if not run before for this application
onSessionStart, if not run before for this session
onRequestStart
onRequest, or the requested page if there is no onRequest method
onRequestEnd
The following methods are triggered by specific events:
onApplicationEnd
onSessionEnd
onError
The onApplicationEnd and onSessionEnd methods do not execute in the context of a page
request, so they cannot access request variables or display information to the user. The
OnError
method does not always execute in the context of a request; you can use its
Event argument to
determine the context.
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 MX 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 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, Macromedia
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 MX Administrator Mappings page. You can, therefore, use this
technique to share utility functions across applications.