English 中文(简体)
Is onApplicationStart is good Idea in ColdFusion?
原标题:

I have to use a Variable(Query Resultset) in ColdFusion, which will get the results from Other Application DB, and stores in Coldfusion Application.

The main idea is that I need to call the other Application DB only at Server startup time and cache the results in local. And I need to read the variable in other pages in my Application. I won t overwrite that variable in any page.

On googling I found that onApplicationStart is useful to assign the variables at Application Startup time.

Is using the onApplicationStart fine or is there any other way? We can assign a variable at startup time(one time).

If onApplicationStart is fine: how to use? Maybe any link where it is explained clearly is helpful.

最佳回答

Well, it depends. How often will this query data be updated? If it really is unchanging, then onApplicationStart() is a fine place to put it. However, if it will change every so often, you can just tell Coldfusion to cache the query for a certain period of time, then you don t need to mess with onApplicationStart(), but rather when you call the query it will return the cached result automatically (within your specified time period).

Regardless, I would write a custom function to retrieve the data. Then it will be trivial to call it from onApplicationStart() or elsewhere.

Startup.cfc: (Named whatever you like)

<!--- Replace the datasource name with your db name --->
<cffunction name="getStartupQuery" hint="Returns a query recordset for startup">
    <cfargument name="datasource" required="no" type="string" default="OtherAppDB">
    <!--- Init the query variable --->
    <cfset var result = queryNew("id")>

    <!-- Get the query dataset --->
    <cfquery name="result" datasource="#arguments.datasource#">
         YOUR QUERY HERE
    </cfquery>

    <cfreturn result>
</cffunction>

Application.cfc: (Just the important parts)

<cffunction name="onApplicationStart">
    <!--- init the startup.cfc, then retrieve the data
    and save it to the application scope. Remember the component name must match
    your component above --->
    <cfset var startup = createObject("component", "startup")>
    <cfset application.varFromOtherDB = startup.getStartupQuery()>
    <cfreturn true>
</cffunction>

Now, you should be able to access this variable from any CFM or CFC in your application using:

<cfset myNewVar = application.varFromOtherDB>
or
#application.varFromOtherDB#

IF you use the onApplicationStart() method, I highly recommend implementing a method to reinit the application. For an example, see this other discussion.

问题回答

暂无回答




相关问题
Checkpointing and restarting X11 applications

I want to checkpoint and restart X11 applications. I am using the BLCR (Berkeley Lab Checkpoint/Restart (BLCR)) tool. BLCR is not able (without modifications) to reinitiate the connection to the X-...

PHP APC uptime problem

I am on LAMP with Alternative PHP Cache (APC). It worked fine until yesterday when I updated the website and changed a few MySQL queries (I don t see how it would affect the APC opcode cache.) Today ...

Android: How to restart an activity within a tabhost?

I ve searched and I know it seems some people frown upon using activities within tabs, but moving past that...how would I restart a tabbed activity while still keeping the tabs visible? I have an ...

Is onApplicationStart is good Idea in ColdFusion?

I have to use a Variable(Query Resultset) in ColdFusion, which will get the results from Other Application DB, and stores in Coldfusion Application. The main idea is that I need to call the other ...

Capistrano not restarting

Capistrano is deploying cold, deploying updates and uploading the symlink fine. It will not however restart. I notice that permission is denied on the /script/process/reaper file. I have found a ...

Wipe SVN to reinitialise

Does anybody know how to delete the complete svn repository including old revisions? I just want to start again ;) Thanks, wishi

热门标签