|
I'm not sure you'd need that double locking for session scoped
variables, but you do need it for application or server scoped
variables if you want to ensure that there is only one instance of that
CFC. Here's why, with another code example using application scope. <cfif not structKeyExists(application,"MyCFC")> <cfset application.myCFC = createObject("component","MyCFC").init() /> </cfif> Say user A hits the above code block without the double "not structKeyExists()" test, and there's not an instance of application.myCFC at that time. They would pass the if "not structKeyExists()" test. Say another user, B, hits that code block "a few milliseconds" later, before the object was created. User B would also pass the "not structKeyExists()" test, and i've heard it said that you can then get 2 instances of the CFC. What i'm not exactly clear on is the consequences of that. Apparently, user A has a pointer to an application scoped CFC that would last for the duration of user A's initial request. On user A's next request, presumably application.myCFC would point to the instance that user B created, and the instance that user A created would be floating in space unused, perhaps taking up memory needlessly until the application expired. There might be other consequences to your application, depending on what that CFC does. Perhaps someone else is more clear on the consequences. Hence the double test locking code is recommended as best practice: <cfif not structKeyExists(application,"MyCFC")> <cflock name="MyCFCLock" type="exclusive" timeout="10"> <cfif not structKeyExists(application,"MyCFC")> <cfset application.MyCFC = createObject("component","MyCFC").init() /> </cfif> </cflock> </cfif> BUT for session scoped variables, it's usually impossible for a single user to hit an "if not structKeyExists(session,"MyCFC") code block twice within the same request, much less hitting it twice within "a few milliseconds". If you're using frames in such a way that makes that possible, then yes, the double locking might be necessary. Otherwise, for session scoped variables, it's generally not needed. A single test will suffice. ciao, Nando Lyons, Larry wrote:
You are subscribed to cfcdev. To unsubscribe, send an email to [email protected] with the words 'unsubscribe cfcdev' as the subject of the email. CFCDev is run by CFCZone (www.cfczone.org) and supported by CFXHosting (www.cfxhosting.com). An archive of the CFCDev list is available at www.mail-archive.com/[email protected] |
- [CFCDev] Best Practices Question, Where to Instantiate a ... Lyons, Larry
- Re: [CFCDev] Best Practices Question, Where to Insta... Ryan Guill
- Re: [CFCDev] Best Practices Question, Where to Insta... Daniel Roberts
- Re: [CFCDev] Best Practices Question, Where to I... Daniel Roberts
- RE: [CFCDev] Best Practices Question, Where to Insta... Lyons, Larry
- Re: [CFCDev] Best Practices Question, Where to I... Daniel Roberts
- Re: [CFCDev] Best Practices Question, Where to I... Nando
- RE: [CFCDev] Best Practices Question, Where ... Geoff Parkhurst
- Re: [CFCDev] Best Practices Question, Wh... Daniel Roberts
- Re: [CFCDev] Best Practices Questio... Peter J. Farrell
- RE: [CFCDev] Best Practices Question, Where to Insta... Dawson, Michael
- Re: [CFCDev] Best Practices Question, Where to I... Peter J. Farrell
- RE: [CFCDev] Best Practices Question, Where to Insta... Tim Blair
