Create datasources in Application.cfc

Hi,

Trying to create datasources in the Application.cfc file as follows:

<!--- set datasources in Lucee  --->  
component {
this.datasources["mydsn"] = {
	  class: 'com.mysql.jdbc.Driver'
	, bundleName: 'com.mysql.jdbc'
	, bundleVersion: '5.1.40'
	, connectionString: 'jdbc:mysql://localhost:3306/db?useUnicode=true&characterEncoding=UTF-8&useLegacyDatetimeCode=true'
	, username: 'theUsername'
	, password: "encrypted:XXXXXXX"
	
	// optional settings
	, blob:true // default: false
	, clob:true // default: false
	, connectionLimit:100 // default:-1
};
}

but it seems to get ignored. I copied the string above directly from Lucee Admininistrator and then I deleted it from Lucee Administrator in order to set in Application.cfc. Any idea what I’m doing wrong here?

please always mention which version of lucee you’re having problems with :slight_smile:

sorry it’s version Lucee 5.2.9.31

@deeztek, Did you face any issues for this? Please added a stack trace.If possible

No stack trace. The datasource is not there and any queries using that datasource I get datasource cannot be found. If I add in the Lucee Administrator, it works with no problem.

Lucee has a “feature” where by it simply ignores any configuration errors in the Application.cfc. I would suggest putting in a ticket perhaps to improve the logging, but first do check all the server and web context logs just to make sure there’s no errors logged to them.

@deeztek, Could you please put this code this.datasource = “YOUR_DSN_NAME” to below of your data source creation code?

component {
	this.datasources[“mydsn”] = {
	class: ‘com.mysql.jdbc.Driver’
	, bundleName: ‘com.mysql.jdbc’
	, bundleVersion: ‘5.1.40’
	, connectionString: ‘jdbc:mysql://localhost:3306/db?useUnicode=true&characterEncoding=UTF-8&useLegacyDatetimeCode=true’
	, username: ‘theUsername’
	, password: “encrypted:XXXXXXX”

	};
	this.datasource = "YOUR_DSN_NAME"
}

@bdw429s I’m not seeing any errors under server or the web context logs besides the following entry:

“ERROR”,“http-nio-8888-exec-3”,“11/26/2019”,“15:26:03”,“”,";datasource [mydsn] doesn’t exist
available datasource names are [];lucee.runtime.exp.DatabaseException: datasource [mydsn] doesn’t exist

@cfmitrah I tried adding the entry as you suggested but I got the same error that the datasource doesn’t exist.

Here’s my Application.cfc:

<cfcomponent output="true">

<cfset this.name="MyQpp">
<cfset this.applicationTimeout = CreateTimeSpan(0,0,60,0)>
<cfset this.sessionTimeout = CreateTimeSpan(0,0,60,0)>
<cfset this.sessionManagement="true">
<cfset this.clientManagement = "false">
  
<!--- begin function: onApplicationStart --->
<cffunction name="onApplicationStart" output="no" returntype="void">

<!--- set datasources in Lucee  --->  
component {
this.datasources["MyDSN"] = {
	  class: 'com.mysql.jdbc.Driver'
	, bundleName: 'com.mysql.jdbc'
	, bundleVersion: '5.1.40'
	, connectionString: 'jdbc:mysql://localhost:3306/db?useUnicode=true&characterEncoding=UTF-8&useLegacyDatetimeCode=true'
	, username: 'theUsername'
	, password: "encrypted:XXXX"
	
	// optional settings
	, blob:true // default: false
	, clob:true // default: false
	, connectionLimit:100 // default:-1
};
this.datasource = "MyDsn"
}

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



<!--- close function: onApplicationStart --->
</cffunction>

     

<!--- function: onRequestStart ---> 

<cffunction name="onRequestStart">
<!--- include Application.cfm --->
<cfinclude template="Application.cfm">  
<!--- close function: onRequestStart --->
</cffunction>
</cfcomponent>

try moving the code into the constructor, ie after <cfset this.clientManagement = "false"> before onApplicationStart

Yes, all “this.foo” settings need to be in the pseudo constructor (outside of any method) but I’m also highly confused as you seem to have a tag-based CFC with a bunch of script-based code slapped inside of a method which isn’t going to work! Firstly, you need cfscript tags around any script code. And secondly, you don’t need the component bits as you’re already inside of a component! I think you’re copying and pasting way too much from the examples.

1 Like

Try something like this:

component {

	this.name="MyQpp";
	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/db?useUnicode=true&characterEncoding=UTF-8&useLegacyDatetimeCode=true'
		, username: 'theUsername'
		, password: "encrypted:XXXX"
		
		// optional settings
		, blob:true // default: false
		, clob:true // default: false
		, connectionLimit:100 // default:-1
	};
	
	this.datasource = "MyDsn";
	
	this.componentpaths["/pop"]= "/opt/lucee/tomcat/webapps/ROOT/WEB-INF/lucee/components/custom/extension/pop4";
	  
	
	function onRequestStart() {
		include "Application.cfm";
	}

}

@bdw429s I copied and pasted your code into an new Application.cfc and substituted the datasource connection parameters. Still not working:

Lucee 5.2.9.31 Error (database)
Message datasource [MyDSN] doesn’t exist
Detail available datasource names are []
Datasource MyDSN

The datasource “MyDSN” is referenced by some legacy code in the Application.cfm that I haven’t converted to the Application.cfc yet however, everything works as expected ONLY if I manually add the datasource in Lucee Administrator.

@bdw429s Additionally, the component mapping is also ignored:

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

Getting the following error when trying to use the component:

Lucee 5.2.9.31 Error (expression)
Message invalid component definition, can’t find component [pop]

Again, in this case, if I enter the component manually, it works with no problem.

So it looks like no matter what I put in the Application.cfc it seems to get ignored.

It’s possible Lucee is finding your Application.cfm first and ignoring the Application.cfc. If you put a dump/abort in the application.cfc, does it fire? I’m not sure the order Lucee looks in, but I’d recommend renaming or removing the Application.cfm.

Isn’t it the other way around? that application.cfc overrules / ignores application.cfm? Could be wrong though.

I’m using this in my application.cfc with succes in Lucee 5.2.9.31 and 5.3.3.62
This example is for MSSQL dough, but principle stays the same.

component {

	this.datasources = 
	{ 
		onecfc= 
		{ 
		database = "one" 
		, host = "10.0.0.100" 
		, port = "1433"
		, class: 'net.sourceforge.jtds.jdbc.Driver'
		, bundleName: 'jtds'
		, bundleVersion: '1.3.1'
		, connectionString: 'jdbc:jtds:sqlserver://10.0.0.100:1433/one'
		, username: 'xxxx'
		, password: "encrypted:xxxx"
		
		// optional settings
		, connectionLimit:100 // default:-1
		, validate:false // default: false
		}
	};

}

@Gunter It could be another “feature” of Lucee where it uses the Application.cfm first.

1 Like

So, I renamed Application.cfm and removed the include to it and it seems to be working in both the datasources and the component. Now, I’m having a new issue. I have a global variable in the application named datasource that was set in the Application.cfm as:

<cfset datasource="MyDatasource">

So removing Application.cfm from the equation has broken this variable and my application is complaining that the variable doesn’t exist.

So, I tried to set it as follows and it’s still not working:

component {
   function onApplicationStart() {
       application.datasource = "MyDatasource";
       }  
}

I tried setting it up as follows with no success either:

component {

	this.name="MyApp";
	this.applicationTimeout = CreateTimeSpan(0,0,60,0);
	this.sessionTimeout = CreateTimeSpan(0,0,60,0);
	this.sessionManagement=true;
	this.clientManagement = false;
       application.datasource = 'MyDatasource';
      ...
}