Edit

Application.cfc

Example: Full Application/Request Lifecycle Methods

This shows all of the built-in Application.cfc methods. Application variables are explained in Adobe's Application.cfc documentation.

component {
    this.name = "YourAppName" & hash(getCurrentTemplatePath());
    this.applicationTimeout = createTimeSpan(1,0,0,0);
    this.sessionTimeout = createTimeSpan(1,0,0,0);
    this.sessionManagement = true;
    this.setClientCookies = false;

    public boolean function onApplicationStart() {
        return true;
    }

    public void function onApplicationEnd(struct applicationScope={}) {
        return;
    }

    public void function onSessionStart() {
        return;
    } 

    public void function onSessionEnd(required struct sessionScope, struct applicationScope={}) {
        return;
    }

    public boolean function onRequestStart(required string targetPage) {
        return true;
    }

    public void function onRequest(required string targetPage) {
        include arguments.targetPage;
        return;
    }

    public void function onCFCRequest(string cfcname, string method, struct args) {
        return;
    }

    public void function onRequestEnd() {
        return;
    }

    public void function onAbort(required string targetPage) {
        return;
    }

    public void function onError(required any exception, required string eventName) {
        return;
    }

    public boolean function onMissingTemplate(required string targetPage) {
        return true;
    }
}

Lifecycle Methods explained

MethodDescription
onApplicationStartFirst function run when ColdFusion receives the first request for a page in the application.
onApplicationEndLast function run when Application times out or server is shut down.
onSessionStartRun when first setting up a session.
onSessionEndRun when a session ends.
onRequestStartFirst page-processing function run when a page request starts. Return False to prevent ColdFusion from processing the request.
onRequestRuns when a request starts, after the onRequestStart event handler. This method is optional. If you implement this method, it must explicitly call the requested page to process it.
onCFCRequestIntercepts any HTTP or AMF calls to an application based on CFC request. Whereas onRequest handles only requests made to ColdFusion templates, this function controls Ajax, Web Service, and Flash Remoting requests.
onRequestEndRuns at the end of a request, after all other CFML code.
onAbortRuns when you execute the CFML tag cfabort or cfscript "abort". If showError attribute is specified in cfabort, onError method is executed instead of onAbort. When using cfabort, cflocation, or cfcontent tags, the onAbort method is invoked instead on onRequestEnd.
onErrorRuns when an uncaught exception occurs in the application. This method overrides any error handlers that you set in the ColdFusion Administrator or in cferror tags. It does not override try/catch blocks.
onMissingTemplateRuns when a request specifies a non-existent CFML page. True, or no return value, specifies that the event has been processed. If the function returns false, ColdFusion invokes the standard error handler.

APPLICATION Scope

The APPLICATION scope is used for setting the information at application level, i.e.

Example using onApplicationStart

component {
    this.name = "myApplication";

    function onApplicationStart() {
        application.something = "otherthing";
    }
}

Locking Concerns

Since variables within the application scope can be accessed by multiple threads at once (simultaneous requests), you should consider if locking is necessary.

In very early versions of ColdFusion (pre-CF6), locking was required when reading or writing to a shared scope, such as the application scope. In modern versions of ColdFusion or Lucee, in most cases, locking is taken care of automatically and you do not need to use cflock.

One case that does not require locking is if you set your application variables in onApplicationStart and do not modify them (i.e., they are only read, except in onRequestStart).

You should take care not to lock unnecessarily, as it may create a performance bottleneck.

One case where you should use locking is when you are using the application scope on both sides of the assignment. For example:

application.counter += 1;

The above example requires locking to ensure that a concurrent thread does not corrupt the value of the counter.

lock scope="application" timeout="1" type="exclusive" {
    application.counter += 1;
}

Keep in mind that, if the application scoped variable is written to at any time other than onApplicationStart, there is a possibility that the value of the application variable could change mid-request. If this is a problem, consider using cflock to ensure a consistent value.

Signup for cfbreak to stay updated on the latest news from the ColdFusion / CFML community. One email, every friday.

Fork me on GitHub