Consider this original function in a CF component:
<cffunction name="getAccount" output="false" returntype="any">
<cfargument name="event" type="MachII.framework.Event" required="yes" />
<cfscript>
var account = createObject("java","com.mycompany.user.Account").init();
// gets user entered email, password from form, sets account bean email address property
account.set( arguments.event.getArgs() );
// load account properties from persistant datasource using email address
getAccountManager().loadAccount( account );
return account;
</cfscript>
</cffunction>
Many threads simultaneously compete for this function as the component is a part of (and accountManager as well) a singleton object. The problem: the initial thread was interrupted at some point in the function by another thread and the second thread overwrote the properties of the account java bean. Then, when the first thread resumed, it would have incorrect properties for the account bean.
I thought I had solved the problem by adding a cflock around the code in this function and in the loadAccount function of accountManager.cfc like so:
<cffunction name="getAccount" output="false" returntype="any">
<cfargument name="event" type="MachII.framework.Event" required="yes" />
<cflock name="getAccountLock" type="exclusive" timeout="10" throwontimeout="true">
<cfset account = createObject("java","com.mycompany.user.Account").init()>
<cfset account.set( arguments.event.getArgs() )>
// also added a cflock within this function to protect its functionality
<cfset getAccountManager().loadAccount( account )>
<cfreturn account>
</cflock>
</cffunction>
...but now I think I still have a problem as the variable 'account' is not in the var scope.
So I thought about adding <cfset var account=""> as the first line of the function (it won't let me add this line within the cflock).
My question is, if I add this line, will secondary threads, when they enter the function and execute the first line then wait for the cflock to release, wipe out the reference to the account bean while the first thread is executing?
<cffunction name="getAccount" output="false" returntype="any">
<cfargument name="event" type="MachII.framework.Event" required="yes" />
<cfscript>
var account = createObject("java","com.mycompany.user.Account").init();
// gets user entered email, password from form, sets account bean email address property
account.set( arguments.event.getArgs() );
// load account properties from persistant datasource using email address
getAccountManager().loadAccount( account );
return account;
</cfscript>
</cffunction>
Many threads simultaneously compete for this function as the component is a part of (and accountManager as well) a singleton object. The problem: the initial thread was interrupted at some point in the function by another thread and the second thread overwrote the properties of the account java bean. Then, when the first thread resumed, it would have incorrect properties for the account bean.
I thought I had solved the problem by adding a cflock around the code in this function and in the loadAccount function of accountManager.cfc like so:
<cffunction name="getAccount" output="false" returntype="any">
<cfargument name="event" type="MachII.framework.Event" required="yes" />
<cflock name="getAccountLock" type="exclusive" timeout="10" throwontimeout="true">
<cfset account = createObject("java","com.mycompany.user.Account").init()>
<cfset account.set( arguments.event.getArgs() )>
// also added a cflock within this function to protect its functionality
<cfset getAccountManager().loadAccount( account )>
<cfreturn account>
</cflock>
</cffunction>
...but now I think I still have a problem as the variable 'account' is not in the var scope.
So I thought about adding <cfset var account=""> as the first line of the function (it won't let me add this line within the cflock).
My question is, if I add this line, will secondary threads, when they enter the function and execute the first line then wait for the cflock to release, wipe out the reference to the account bean while the first thread is executing?