Application.cfc onRequestStart() Called Twice

Posted: September 30, 2012 in ColdFusion

I spent a bit of time tracking down why I kept seeing two exact same threads (run by thread()) being run simultaneously. These threads are spawned by FrameWork One’s (FW/1) setupRequest() method. After tracing the methods with some console dumps, doing some research via Google and process of elimination, I finally got a clue.

Let’s say I hit http://mysite.com and this site requires a login. If you are not logged in lets saw we use a location() call to get to the login page. I also use a log-in imitator via a UI to see how different account look under their login, which also uses the location() method. Because of these location() calls, the onRequestStart method is called once before the redirect and once after the redirect.

Now, thinking I can overcome this by wrapping the thread() call in if( !isDefined( “myThreadName” ) ). However – apparently the onRequestStart calls come so quick that the thread name isn’t created in time to be detected. So two threads are still spawned.

I could probably add a flag to the session scope wrapped in a lock operator. However what happens if the thread is terminated manually? This may not be the best option, but it’s not bad as long as your site admins are aware of that consequence.

In this case I’m going to reassign my statements to my log-in handler instead of my setupRequest() method and call it good.

Let me know if you have any other ideas on how to handle this. I’m all ears.

Advertisements
Comments
  1. Well… yeah, you would see two onRequestStart() calls if you do a relocate… a location() call causes another request to be made: that’s how it works.

    A location() or CFLOCATION is not a server-side “GOTO”, it sends a 30x response back to the browser, telling it to redirect to the specified URL and request that instead.

    If you don’t want that to happen, use some mechanism other than a location().


    Adam

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s