Create datasources in Application.cfc

Put this in your onRequest function and the variable will be available to any script within your application:

<cfcomponent>

<cffunction name="onRequest">
  <cfargument name="targetpage" type="string" required=true>
  <cfset datasource="MyDatasource">
  <cfinclude template="#targetpage#">
</cffunction>

</cfcomponent>

Just adding some information: Simply renaming Application.cfm in Application.cfc may break your code because Application.cfm is the old classic way of creating Applications-Settings, while Application.cfc is new and have lot of new methods added, making it a way more powerfull because you can hook your code to very specific processing steps. For example:

  • onApplicationStart
  • onRequestStart
  • onServerStart
  • onSessionStart
    etc…

The downside is, you have to translate/transpose your old classic code to the right sections/methods.

As an example:
While in a classic application.cfm you could just

<cfset myscope.somevar="foobar">

making the variable available to every cfm-template of the application, in a modern Application.cfc this may throw an error because you must place that code in a applicationOnmethod that works. In your case, move your global var to:

<cffunction name="onRequest">
<cfset datasource="MyDatasource">
</cffunction>

So what you should do before renaming your application.cfm to cfc is to set up a basic application.cfc structure first and relocate your old classic code to the right places of the application.cfc to work.

A very good starting point is
Bennadels Rockin’ Application.cfc tutorial

I did not rename Application.cfm to Application.cfc. Sorry I didn’t make that clear. I’m trying to convert my Application.cfm code to Application.cfc and it has been very frustrating to say the least:

Here’s my Application.cfc which does NOT work. I’m getting datasource [MyDSN] doesn’t exist error in Lucee even though it’s CLEARLY set below:

component {

this.name="MyApp";
this.applicationTimeout = CreateTimeSpan(0,0,60,0);
this.sessionTimeout = CreateTimeSpan(0,0,60,0);
this.sessionManagement=true;
this.clientManagement = false;
   

 
   this.datasources["MyDSN"] = {
  class: 'com.mysql.jdbc.Driver'
, bundleName: 'com.mysql.jdbc'
, bundleVersion: '5.1.40'
, connectionString: 'jdbc:mysql://localhost:3306/theDB?useUnicode=true&characterEncoding=UTF-8&useLegacyDatetimeCode=true'
, username: 'theUsername'
, password: "thePassword"

// optional settings
, blob:true // default: false
, clob:true // default: false
, connectionLimit:100 // default:-1
   };

  
   // Initialize POP component
   this.componentpaths["/pop"]= "/opt/lucee/tomcat/webapps/ROOT/WEB-INF/lucee/components/custom/extension/pop4";

}

component {

  //Authentication Section
  <cfcomponent> 
  <cfset This.name = "AdminGUI" /> 
  <cfset This.Sessionmanagement="True"> 
  <cfset This.loginstorage="session">\
  <cfset datasource="MyDatasource"> 
  <cffunction name="OnRequestStart"> 
  <CFPARAM NAME="session.Loggedin" DEFAULT="FALSE">
  <CFIF #session.Loggedin# IS "FALSE">
  <cfinclude template="logon.cfm"> 
  <cfabort>
  </CFIF>
  </cffunction> 
  </cfcomponent>

}

If I remove the section below I’m not getting the error datasource [MyDSN] doesn’t exist but then my authentication doesn’t work and I’m getting variable [DATASOURCE] dosn’t exist error:

component {

  //Authentication Section
  <cfcomponent> 
  <cfset This.name = "AdminGUI" /> 
  <cfset This.Sessionmanagement="True"> 
  <cfset This.loginstorage="session">\
  <cfset datasource="MyDatasource"> 
  <cffunction name="OnRequestStart"> 
  <CFPARAM NAME="session.Loggedin" DEFAULT="FALSE">
  <CFIF #session.Loggedin# IS "FALSE">
  <cfinclude template="logon.cfm"> 
  <cfabort>
  </CFIF>
  </cffunction> 
  </cfcomponent>

}

If I try to separate the cfset datasource=“MyDatasource”> in its own section I’m starting to get weird errors that the name is missing from the cfcomponent even though I clearly define it.

All I want to do is the following:

Define a datasource to my database, defined a component mapping, set my authentication and set a global variable called datasource. It shouldn’t be this hard but it’s kicking my butt.

From what I understand, try this code:

Appication.cfc

<cfcomponent displayname="Application" output="false" hint="Handle the applications">
	<cfscript>
		this.datasources["MyDSN"] = {
		class: 'com.mysql.jdbc.Driver'
		, bundleName: 'com.mysql.jdbc'
		, bundleVersion: '5.1.40'
		, connectionString: 'jdbc:mysql://localhost:3306/theDB?useUnicode=true&characterEncoding=UTF-8&useLegacyDatetimeCode=true'
		, username: 'theUsername'
		, password: "thePassword"
		// optional settings
		, blob:true // default: false
		, clob:true // default: false
		, connectionLimit:100 // default:-1
		};
	</cfscript>

	<cfset This.name="AdminGUI" />
	<cfset This.Sessionmanagement="True" />
	<cfset This.loginstorage="session" />
	<cfset This.componentpaths["/pop"]= "/opt/lucee/tomcat/webapps/ROOT/WEB-INF/lucee/components/custom/extension/pop4" />

	<cffunction name="onRequest">

		<cfargument name="targetPage" type="String" required=true />

		<cfparam name="session.Loggedin" default=false />
		<cfset datasource="MyDatasource" />

		<cfif not session.Loggedin>
			<cfinclude template="logon.cfm" />
			<cfabort />
		</cfif>

		<cfinclude template="#Arguments.targetPage#" />

		<cfreturn />

	</cffunction>

</cfcomponent>

I would also define that scope precisely, e.g.:

<cfset myGlobalApp.datasource="MyDatasource" />

And make a reference in the template code like:

<cfquery  name="foobar" dsn="#myGlobalApp.datasource#">
SELECT foo_field
FROM    bar_table
WHERE ID=#somerequest.id#
</cfquery>

I’ve just updated the code above, because I forgot the logon.cfm section.