Application.cfm (used to persist object - needed) ===================================================== <cfapplication name="testcomps">
<cfif not isDefined("application.objApp")> <cfset application.objApp = createObject("component","test")> <cfset OK = application.objApp.init()> </cfif>
First off, this isn't thread-safe. application.objApp can exist but not be initialized (one thread executes the cfif and the first cfset, but before it executes the second cfset another thread comes in and finds application.objApp is defined so it goes ahead and uses the - uninitialized - object).
I recommend changing your init() method to have a returntype= attribute that matches the component type ("test" or "app" in your case - you seem to have both?!?!) and adding a <cfreturn this/> line at the end of the init() method. Then you can do your initialization in one line:
<cfset application.objApp = createObject("component","app").init()/>
That makes the above code thread-safe (except that your initialization can happen in more than one thread). Truly thread-safe code would look like this:
<cfif not structKeyExists(application,"objApp")>
<cflock name="testcomps_application_objApp" type="exclusive" timeout="10">
<cfif not structKeyExists(application,"objApp")>
<cfset application.objApp = createObject("component","app").init()/>
</cfif>
</cflock>
</cfif>
You need the double-cfif to fully avoid race conditions.
app.cfc:
========
<cfcomponent>
<cfproperty name="testval" type="string">
<cfproperty name="teststruct" type="struct"
default="StructNew()">
<cfproperty name="teststruct.value1" type="string" default="">
<cfproperty name="teststruct.value2" type="string" default="">
Are you aware that <cfproperty> does *nothing* (unless you are declaring information for a web service return type)?
In particular, this does *not* set default values for instance variables. Unless you are writing web services with complex return types, I'd strongly recommend not using <cfproperty>.
<cffunction name="init" access="public" output="No"> <cfset this.testval = "hello world"> <cfset this.teststruct.value1 = "fido was here"> <cfset this.teststruct.value2 = "and here"> </cffunction>
</cfcomponent>
Hope that helps...
Regards, Sean
--- You are currently subscribed to cfaussie as: [EMAIL PROTECTED] To unsubscribe send a blank email to [EMAIL PROTECTED]
MXDU2004 + Macromedia DevCon AsiaPac + Sydney, Australia http://www.mxdu.com/ + 24-25 February, 2004
