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.

This is what I use. I pass all data as environmental vars. Run this in the onApplicationStart section. I hope this helps.

ā€“ Bill


<!--- onApplicationStart --->
<cffunction name="onApplicationStart" returnType="boolean" output="false">

  

 <cfadmin action="updateDatasource" type="server" password="#variables.admin_password#" classname="org.postgresql.Driver" dsn="jdbc:postgresql://{host}:{port}/{database}" name="dsn_ltt_app_read" newname="dsn_ltt_app_read" host="#variables.db_host#" database="#variables.db_name#" port="5432" dbusername="#variables.db_user#" dbpassword="#variables.db_password#" connectionLimit="" connectionTimeout="1" blob="false" clob="false" allowed_select="true" allowed_insert="true" allowed_update="true" allowed_delete="true" allowed_alter="true" allowed_drop="true" allowed_revoke="true" allowed_create="true" allowed_grant="true">
  <cfadmin action="updateDatasource" type="server" password="#variables.admin_password#" classname="org.postgresql.Driver" dsn="jdbc:postgresql://{host}:{port}/{database}" name="dsn_ltt_app_write" newname="dsn_ltt_app_write" host="#variables.db_host#" database="#variables.db_name#" port="5432" dbusername="#variables.db_user#" dbpassword="#variables.db_password#" connectionLimit="" connectionTimeout="1" blob="false" clob="false" allowed_select="true" allowed_insert="true" allowed_update="true" allowed_delete="true" allowed_alter="true" allowed_drop="true" allowed_revoke="true" allowed_create="true" allowed_grant="true">