質問

I have a component "bulletin.cfc" which contains lots of functions.

My main page has two threads running on it using the cfthread tag.

Coming from .net I thought I would create two instrances of the component, and use one in each thread. This way they wouldnt mess with each other and I wouldnt need to worry about putting locks in the functions.

<cfset bullObj = new bulletin()>
<cfset bullObj2 = new bulletin()>

Is this correct?

EDIT:

Thanks for the answers so far, I still can't understand a problem that is happening with this though. I have the following code inside two seperate cfthread elements:

<cfset listCount = 1>
    <cftry>
    <cfquery name="ins" datasource="#datasourceVar#" >
        INSERT INTO element_user_shown
        (elementid, userid, date_shown)
        (
        <cfloop list="#elementIDList#" index="lcv">
        SELECT #lcv#, #tmpuserid#, GETDATE()
            <cfif listCount LT listlen(elementIDList)>
                UNION ALL
            </cfif>
        <cfset listCount = listCount + 1>
        </cfloop>   
        )               
    </cfquery>  

This runs about 70,000 times a night but gets about 3-4 errors each time. Checking the sql that errors it looks like

        INSERT INTO element_user_shown
                        (elementid, userid, date_shown)
                        (

                            SELECT 621, 267509, GETDATE()

                            UNION ALL

                            SELECT 586, 267509, GETDATE()

                            UNION ALL

                            SELECT 594, 267509, GETDATE()

                            UNION ALL

                            SELECT 613, 267509, GETDATE()

                            SELECT 622, 267509, GETDATE()

                            SELECT 599, 267509, GETDATE()

                            SELECT 602, 267509, GETDATE()                               

                        )
役に立ちましたか?

解決

You are correct that your instances are passed by reference so the potential might be there to have a concurrency problem. But if your function arguments are properly paramed and you are not changing properties as a part of the instance (in other words this is an interface and not a bean) you can safely reuse the same instance. Each function call is it's own scope and variables within and returned are for the life of the function call.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top