Skip to main content
Known Participant
January 25, 2024
Answered

CFLock not throwing exception

  • January 25, 2024
  • 2 replies
  • 575 views

Why does the following code not throw an exception when timeout is exceeded?
CF Admin "Timeout Requests after seconds" is checked and set to 2 seconds.


<cftry> 
  <cflock scope="application" type="exclusive" timeout="2" throwontimeout="yes">
    <cfset sleep(3000)>
  </cflock>
  <cfcatch type="any">  
  <h1>My Exception</h1>  
    <cfdump var="#cfcatch.type#"> 
    <cfdump var="#cfcatch.Message#"> 
  </cfcatch>
</cftry>  


 

    This topic has been closed for replies.
    Correct answer EddieLotter

    No exception is thrown because the timeout is for acquiring the lock, not what is being protected by the locked.

    In your example, the lock has been successfully acquired when execution reaches the sleep() function.

    2 replies

    BKBK
    Community Expert
    Community Expert
    January 26, 2024

    Anyway, you have to test it not with one request thread, as you have done. But instead with at least 2 separate request threads. That is because we are in the application scope.

     

    We would then have a test where one request-thread waits on another. For example, test with page1.cfm and page2.cfm as follows. Launch page1.cfm then, within several seconds, launch page2.cfm.

     

    <!--- page1.cfm --->
    <h2>
    	Sleep it off!
    </h2>
    <cflock scope="application" type="exclusive" timeout="30" throwontimeout="yes">
    	<cfset sleepItOff()>
    </cflock>
    
    <cffunction name="sleepItOff">
    	<!--- A good, long sleep! --->
    	<cfset sleep(30000)>
    </cffunction>
    <!--- page2.cfm --->
    <cftry> 
    <cfset requestStartTime=getTickCount()>
    <!--- You will have to wait for application that sleeps for 30 seconds --->	
    <cflock scope="application" type="readonly" timeout="2" throwontimeout="yes">
    	<cfset getIt()>
    </cflock>
    
    <cfcatch type="any">  
    	<h1 style="color:purple">
    	<cfoutput>Request waited for #(getTickCount() - requestStartTime)/1000# seconds</cfoutput>
        </h1>
    	<h1>My Exception:</h1>  
    	<cfdump var="#cfcatch#"> 
    </cfcatch> 
    </cftry> 
    
    <cffunction name="getIt">
      	<cfreturn "It">
    </cffunction> 
    

     

     

     

    web-engAuthor
    Known Participant
    January 26, 2024

    Thanks BKBK,
    I got it!

    EddieLotter
    EddieLotterCorrect answer
    Inspiring
    January 25, 2024

    No exception is thrown because the timeout is for acquiring the lock, not what is being protected by the locked.

    In your example, the lock has been successfully acquired when execution reaches the sleep() function.

    web-engAuthor
    Known Participant
    January 25, 2024

    Thanks Eddie,
    I misconstrued "Aquiring the lock".
    Makes sense now.

    EddieLotter
    Inspiring
    January 25, 2024

    You're welcome. Glad to help. 🙂