Nathan Strutz wrote:
> Ian,
>
> It looks like you're not joining your thread back to the main request's
> thread.
>
> You can use cfthread to make a throw-away thread, like you're doing in your
> example, but if you want to use anything created in that spun-off thread,
> your thread either has to write to a shared scope, like application.* or
> session.*, or it has to join back to the main thread.
>   

I was writing to a shared scope.  My code example showed me using the 
application scope.  And I am also joining the threads with <cfthread 
action="join" name="#structKeyList(cfthread)#" /> after my loop, but I 
did not show that in my original posting.

The problem was a race condition since I was not passing in the current 
loop iteration data to each thread so they where all using the last loop 
value.  I needed to pass in the current loop values into each thread as 
attributes and use those when doing my query and writing the results to 
the global application variable.

    <cfloop query="tables">
        <cfthread action="run" owner="#tables.owner#" 
table="#tables.table_name#" name="thd_#tables.owner#_#tables.table_name#">
            <cfset var dbError = "">
            <cfset var count = queryNew('count')>
           
            <cftry>
                <cfquery datasource="extdm" name="count">
                    SELECT COUNT(*) as count
                    FROM #attributes.owner#.#attributes.table#
                </cfquery>
               
                <cfcatch type="database">
                    <cfset dbError = duplicate(cfcatch)>
                </cfcatch>
            </cftry>

      <cflock name="extdm_write" type="exclusive" timeout="5">
                <cfset 
application.extdm[arrayLen(application.extdm)]['tables'][attributes.owner 
& "|" & attributes.table] = structNew()>
                <cfset 
application.extdm[arrayLen(application.extdm)]['tables'][attributes.owner 
& "|" & attributes.table]['count'] = count.count>
                <cfset 
application.extdm[arrayLen(application.extdm)]['tables'][attributes.owner 
& "|" & attributes.table]['dbError'] = duplicate(dbError)>
            </cflock>

        </cfthread>
    </cfloop>
   
    <cfthread action="join" name="#structKeyList(cfthread)#" />



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Adobe® ColdFusion® 8 software 8 is the most important and dramatic release to 
date
Get the Free Trial
http://ad.doubleclick.net/clk;192386516;25150098;k

Archive: 
http://www.houseoffusion.com/groups/CF-Talk/message.cfm/messageid:304521
Subscription: http://www.houseoffusion.com/groups/CF-Talk/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4

Reply via email to