Concurrency when using the Application scope
In Adobe ColdFusion, if
<cfset Application.obj = CreateObject("component","ComponentName")>
<cfset myResult = Application.obj.FunctionName()>
I'm concerned that a var declared in the function's local scope might have concurrency problems because Application.obj is stored in the Application scope.
<cffunction name="FunctionName">
<cfset var local = {}>
(pretend some long process happens here)
<cfif condition>
<cfset local.result = True>
<cfelse>
<cfset local.result = 开发者_StackOverflowFalse>
</cfif>
<cfreturn local.result>
If two people are in that function at the same time, will the result for person 1 corrupt the result for person 2?
To avoid concurrency issues, instantiate the object in the onapplicatiomstart method of your application.cfc. That will ensure the object gets created only once. Second as long as the variable "condition" is also scoped to the local scope, the two calls should not interfere with each other.
As long as all of the variables being accessed are locally scoped (var'd in the function they're being called from, or an argument to that function), there's no concurrency issues. If you are hitting variables.somevar
or this.something
(or just somevar
that doesn't belong to the local scope), then you might start to run into problems.
We do a whole lot of that sort of work.
Yes, there is the potential for a race condition in your code sample.
You will need to use a lock around
<cfset myResult = Application.obj.FunctionName()>
to prevent the race condition.
The type of lock to use would depend really on what the long process is doing.
If you are instantiating your framework you might consider double-checked locking. (Joe Rinehart, author of Model-Glue had a great post on this but his site isn't responding atm.)
If the long process is less critical you could use a simpler lock.
精彩评论