> I have made sure that each request is in the same application, but the
> second request still waits for the previous request to complete.

Right, that's because you put the conditional inside the cflock pair --
so it doesn't test the flag until it's already made sure there isn't
another concurrent instance running, and since you reset the flag at the
end, then once it releases the lock, it's already told the 2nd one "it's
fine, go ahead". 

What you probably want is something more like this: 

<cflock name="SchedLock" timeout="5" type="exclusive">
  <cfparam name="application.SchedLock" default="true" />
  <cfset variables.SchedLock = application.SchedLock />
  <cfif variables.SchedLock>
    <cfset application.SchedLock = false />
  </cfif>
</cflock>

<cfif variables.schedLock>
<!--- it wasn't already running and we locked it, so run it now --->
<cftry>
  [CODE HERE]

  <cfcatch>
    <cflock name="SchedLock" type="exclusive" timeout="5">
      <cfset application.schedLock = true />
    </cflock>
    [HANDLE THE ERROR HERE]
  </cfcatch>
</cftry>

<cflock name="SchedLock" type="exclusive" timeout="5">
  <cfset application.schedLock = true />
</cflock>
</cfif>

So in this case what you're doing is locking access to the application
scope flag variable that tells you whether or not it's running. It
enters the lock, parameterizes the variable and then sets a local
variable to tell the current page whether or not it can execute the code.
If the current page can execute the code, it sets the application flag
so that other requests will skip execution. Then at the end, either if
the code errors or if it completes, it resets the application flag to
release the lock and allow other requests to execute the code. 

Generally speaking, you don't want to have large bodies of code
executing inside a cflock pair anyway. What you want is to use cflock
around just a very small set of a few, fast-running lines where you're
doing something critical. So if you need it to do something like this
where you're using it to single-thread a long-running process, you
separate out the flag-setting from the process itself and just lock the
flag, that way individual requests still don't wait long for the lock and
ideally actually the locks don't throw any errors. So even though you've
got say 3 requests trying to execute the same process and you only want
one of them to complete, as far as the code is concerned, nothing ever
"failed" (generate an error), it's all normal operation. 

hth,
ike

-- 
s. isaac dealey  ^  new epoch
 isn't it time for a change? 
     ph: 503.236.3691

http://onTap.riaforge.org/blog



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Adobe® ColdFusion® 8 software 8 is the most important and dramatic release to 
date
Get the Free Trial
http://ad.doubleclick.net/clk;160198600;22374440;w

Archive: 
http://www.houseoffusion.com/groups/CF-Talk/message.cfm/messageid:298673
Subscription: http://www.houseoffusion.com/groups/CF-Talk/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4

Reply via email to