'ColdFusion Application datasource Property not accessible in CFM Template
The following is running on a ColdFusion 2018 server (in the event this is a version-specific issue).
I'm setting the Application datasource property in the onApplicationStart() LifeCycle Handler, but the datasource property isn't accessible in a CFM Template.
I'm thinking it may have something to do with how the this Scope is handled inside the onApplicationStart() method, but I'm not certain. I tried setting the datasource property using this.datasource as well as Application.datasource, but it's not accessible in the CFM Template either way.
Application.cfc
// The usual App config stuff here... (omitted for brevity)
// Instantiate Instance of Java System Object used to set System Env Vars
this.System = createObject("java", "java.lang.System");
// Include Config files
include "resources/config/AppSettings.cfm";
include "resources/config/onApplicationStart.cfm";
AppSettings.cfm
if (! isDefined(this.System.getProperty("DB_DSN_CREATED")))
{
// Code to read values from .env file here ... (omitted for brevity)
// Set System Env Vars
this.System.setProperty("DB_USER_NAME", "DB USERNAME FROM .ENV FILE HERE");
this.System.setProperty("DB_USER_PASSWORD", "DB PASSWORD FROM .ENV FILE HERE");
}
onApplicationStart.cfm
if (! isDefined(this.System.getProperty("DB_DSN_CREATED")))
{
this.datasources = {MY_DSN = { PROPS FOR DB CONNECTION HERE }};
// *** NOTE: This is the Property that isn't accessible in the CFM Template
// I also tried Application.datasource, but that didn't work either
this.datasource = "MY_DSN";
this.System.setProperty("DB_DSN_CREATED", true);
}
db-test.cfm
<cfscript>
variables.appInstance = createObject('component', 'Application');
variables.sql = "SQL STATEMENT HERE";
variables.sqlParams = {};
// *** NOTE: variables.appInstance.datasource below isn't accessible
// I also tried Application.datasource but that didn't work either
variables.sqlResult = queryExecute(variables.sql, variables.sqlParams, {datasource = variables.appInstance.datasource});
writeDump(variables.sqlResult);
</cfscript>
Does anyone see what I'm missing? Thanks in advance for any guidance!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
