Search code examples
coldfusioncfcapplication.cfc

Issue with application.cfc showing message "Variable PRIMARYDATASOURCE is undefined" for datasource


When I try to fire simple select query in index.cfm, using a datasource defined in Application.cfc it shows an error message. Can anyone help me with proper code sequence?

Error:

Variable PRIMARYDATASOURCE is undefined

Application.cfc

<cfcomponent>

    <!--- Set up the application. --->
    <cfscript>
        this.name = ""; // app name from old cfapplication tag
        this.sessionManagement = "Yes";
        this.loginstorage="session";
        this.setClientCookies = "Yes";
        this.primarydatasource = "diet";
        application.PRIMARYDATASOURCE = "diet";
    </cfscript>

    <cfsetting requesttimeout="100000" />

    <cffunction name="onApplicationStart">
            ....

        <cfquery datasource="#application.PRIMARYDATASOURCE#" name="getCostDetails">
            select * from maimun.adCostDetails ORDER BY ID DESC
        </cfquery>
            ....
    </cffunction>

</cfcomponent>

Solution

  • Move line application.PRIMARYDATASOURCE = "diet"; to the onApplicationStart function and remove line this.primarydatasource = "diet";. Done.

    Code placed in the body of Application.cfc simply isn't executed the way you think it is. You can only access the application scope once the application has been initialized - and onApplicationStart is the event for that. Consider it as the constructor of your web application.