Copy link to clipboard
Copied
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>
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Thanks Eddie,
I misconstrued "Aquiring the lock".
Makes sense now.
Copy link to clipboard
Copied
You're welcome. Glad to help. 🙂
Copy link to clipboard
Copied
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>
Copy link to clipboard
Copied
Thanks BKBK,
I got it!