<!---Application.cfm--->

<cfif not isDefined("application.Counter")>
 <cflock name="Counter" timeout="5" type="EXCLUSIVE">
  <cfif not isDefined("application.Counter")>
   <cfset application.Counter = createObject("component","Counter").init()
/>
  </cfif>
 </cflock>
</cfif>

 

<!---Counter.cfc--->

<cfcomponent>

 <cffunction name="init">
  <cfset _counter = 0 />
  <cfreturn this />
 </cffunction>
 
 <cffunction name="increment">
  <cfset _counter = _counter + 1 />
 </cffunction>
 
 <cffunction name="getCount">
  <cfreturn _counter />
 </cffunction>
 
</cfcomponent>

 

<!---somePage.cfm--->

<cfoutput>#application.Counter.getCount()#</cfoutput><br />
<cfset application.Counter.increment() />

 

If you were to just write in a page <cfset application.Counter._counter =
application.Counter._counter + 1 />   (I know that's horrible and you would
never do that but nevermind that for a second) rather than calling a method
in the CFC then you would need <cflock> that write. But calling the CFC
method writes to the same variable, and (am I wrong about this?) there's no
need to <cflock> the write in the increment() function. Is Java taking care
of the concurrency issue here for me?

<<application/ms-tnef>>

Reply via email to