Application.cfc best practices?

ColdFusion doesn’t provide any direct mechanism for one application to touch another application in memory. Typical solutions to the problem you have would be:

  • Have the parent application hit the sub apps via CFHTTP on a URL that forces a reload. Note this can get tricky with more than one server behind a load balancer
  • Update the time stamp on a shared file that each app checks periodically to force a refresh
  • Use a shared scope that’s visible to all applications on the server such as the server scope and update a timestamp or flag that each sub app checks.

In each of these cases, you still need to be caution of race conditions so 10 threads don’t all try and initiate a refresh at the same time. The standard solution for this is to employ the “double checked” lock like so:

// Check if app needs to reload (many threads can get inside this if at the same time)
if( needsReload() ) {

  // First thread to acquire this lock wins the rights to reinit the app.  All other threads wait here
  // Ensure timeout is greater than time to reload unless you want to have a fail-fast for waiting threads
  lock type="exclusive" name="application_myApp_reloading" timeout="60" {

    // Once the lock is acquired, check if another thread already beat you here (if so, nothing to do)
    if( needsReload() ) {

      // Actually perform the reload-- only one thread gets here.
      performActualReload();

    }

  }

}
2 Likes