Skip to main content
Known Participant
Jan 25, 2024 1:54 AM UTC
Answered

CFLock not throwing exception

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 is closed to new replies. Start a new post to keep the conversation going.
    EddieLotter
    EddieLotterCorrect answer
    Inspiring
    Jan 25, 2024 3:21 PM UTC

    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.

    5 replies

    BKBK
    Community Expert
    Community Expert
    Jan 26, 2024 3:44 PM UTC

    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> 
    

     

     

     

    Translate
    web-engAuthor
    Known Participant
    Jan 26, 2024 8:46 PM UTC

    Thanks BKBK,
    I got it!

    Translate
    EddieLotter
    EddieLotterCorrect answer
    Inspiring
    Jan 25, 2024 3:21 PM UTC

    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.

    Translate
    web-engAuthor
    Known Participant
    Jan 25, 2024 9:58 PM UTC

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

    Translate
    EddieLotter
    Inspiring
    Jan 25, 2024 10:39 PM UTC

    You're welcome. Glad to help. 🙂

    Translate