How to best configure coldbox for our application?

I’m trying to side-by-side our current application codebase so that we can have the old codebase running and develop new modules in coldbox form. One of the quirks is that our datasource is set into the Application scope onApplicationStart() I know this method is pretty common in production environments so that codebase is synced. I just can’t figure out how to do this in coldbox. From my understanding of the docs, I believe I should be implementing the detectEnvironment() method in Coldbox.cfc that returns a string “dev” or “live”. which maps to one of the new methods called dev() and live(). which sets datasource={“dev”=“dsn_dev”} or datasource={“live”=“dsn_live”}, mapping to the CFIDE/adminstator dsn’s am i correct so far?

I copied my code based off of the samples ormexample.

I implemented my models/contacts.cfc:

component extends=“coldbox.system.orm.hibernate.ActiveEntity” table=“contact”{

property name=“contact_id” column=“contact_id” fieldtype=“id” generator=“increment”;
property name=“firstname” column=“firstname” fieldtype=“varchar”;
property name=“lastname” column=“lastname” fieldtype=“varchar”;
property name=“company_id” column=“company_id” fieldtype=“numeric”;

function init(){
return this;
}
}

and my handler/contacts.cfc

component output=“false” {

property name=“restrictions” inject=“model:Contact”;
property name=“contactService” inject=“entityService:Contact”;

public function index(event) {
var rc = event.getCollection();
var prc = event.getCollection(private=true);

prc.contact = contactService.criteriaQuery(criteria=[], sortOrder=“id ASC”, asQuery=false);

event.setView(“contacts/index”);
}
}

and my view/contacts/index.cfm

Showing contacts.

what am i doing wrong? i’m getting exception

coldfusion.runtime.UndefinedElementException: Element DATASOURCE is undefined in SETTINGS.
	at coldfusion.runtime.DotResolver.resolveSplitNameInMap(DotResolver.java:109)
	at coldfusion.runtime.CfJspPage._resolve(CfJspPage.java:1643)
	at coldfusion.runtime.CfJspPage._resolveAndAutoscalarize(CfJspPage.java:1822)
	at coldfusion.runtime.CfJspPage._resolveAndAutoscalarize(CfJspPage.java:1815)
	at cfCFORMUtil2ecfc313866456$funcGETDEFAULTDATASOURCE.runFunction(/home/tcprod/yruan/projects/5.1.3_Winter_Games/coldbox/system/orm/hibernate/util/CFORMUtil.cfc:94)
	at coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:472)
	at coldfusion.runtime.UDFMethod$ReturnTypeFilter.invoke(UDFMethod.java:405)
	at coldfusion.runtime.UDFMethod$ArgumentCollectionFilter.invoke(UDFMethod.java:368)
	at coldfusion.filter.FunctionAccessFilter.invoke(FunctionAccessFilter.java:55)
	at coldfusion.runtime.UDFMethod.runFilterChain(UDFMethod.java:321)
	at coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:220)
	at coldfusion.runtime.CfJspPage._invokeUDF(CfJspPage.java:2659)
	at cfCFORMUtil2ecfc313866456$funcGETENTITYDATASOURCE.runFunction(/home/tcprod/yruan/projects/5.1.3_Winter_Games/coldbox/system/orm/hibernate/util/CFORMUtil.cfc:67)
	at coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:472)
	at coldfusion.runtime.UDFMethod$ReturnTypeFilter.invoke(UDFMethod.java:405)
	at coldfusion.runtime.UDFMethod$ArgumentCollectionFilter.invoke(UDFMethod.java:368)
	at coldfusion.filter.FunctionAccessFilter.invoke(FunctionAccessFilter.java:55)
	at coldfusion.runtime.UDFMethod.runFilterChain(UDFMethod.java:321)
	at coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:220)
	at coldfusion.runtime.TemplateProxy.invoke(TemplateProxy.java:655)
	at coldfusion.runtime.TemplateProxy.invoke(TemplateProxy.java:444)
	at coldfusion.runtime.TemplateProxy.invoke(TemplateProxy.java:414)
	at coldfusion.runtime.CfJspPage._invoke(CfJspPage.java:2432)
	at cfVirtualEntityService2ecfc558745402$funcINIT.runFunction(/home/tcprod/yruan/projects/5.1.3_Winter_Games/coldbox/system/orm/hibernate/VirtualEntityService.cfc:67)
	at coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:472)
	at coldfusion.runtime.UDFMethod$ReturnTypeFilter.invoke(UDFMethod.java:405)
	at coldfusion.runtime.UDFMethod$ArgumentCollectionFilter.invoke(UDFMethod.java:368)
	at coldfusion.filter.FunctionAccessFilter.invoke(FunctionAccessFilter.java:55)
	at coldfusion.runtime.UDFMethod.runFilterChain(UDFMethod.java:321)
	at coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:220)
	at coldfusion.runtime.TemplateProxy.invoke(TemplateProxy.java:655)
	at coldfusion.runtime.TemplateProxy.invoke(TemplateProxy.java:444)
	at coldfusion.runtime.TemplateProxy.invoke(TemplateProxy.java:414)
	at coldfusion.runtime.CfJspPage._invoke(CfJspPage.java:2432)
	at cfBuilder2ecfc1126468659$funcGETENTITYSERVICEDSL.runFunction(/home/tcprod/yruan/projects/5.1.3_Winter_Games/coldbox/system/ioc/Builder.cfc:398)




thanks

The datasource still needs to exist in Application.cfc as wrll.

so in Application.cfc I should set a dummy this.datasource=“dsn_dev” will this be overriden by coldbox.cfc’s datasource.mydsn.name=“dsn_prod” in the live environment?

It only needs to exist in App.cfc for legacy code.

No, if you’re using ORM you will need to ALSO change this when you change the Datasource.

I haven’t found an elegant CB solution for this either, in the meantime I’ve gone with:
Near the top of Application.cfc
if( cgi.local_host eq “production1” || cgi.localhost_eq “production2” ) {
DSN = “productionDSN”;
} else {
DSN = “devDSN”;
}

then later…
this.ormsettings = {
datasource = DSN.

}

Anyone else have better ideas? (Other than obvious go-to of build scripts that replace file strings when deploying to production)

I wonder if there’s a way ColdBox can update the datasource in the application settings in a similar fashion to how we add ColdFusion mappings on-the-fly for modules.

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

Brad,

Yeah I looked at this for years, problem is that this is usually setup in ColdFusion, well before ColdBox is loaded. In other words I gave up trying a few years ago!

In this case the actually problem the OP is having, is with the ORM Settings themselves, I have blogged this problem many times in the past. The best an easiest solution for doing this with multiple sites and DSN’s is to write a model that pulls the required information from whatever source it needs to be pulled from. Which means NO ORM in this situation and plain SQL can work. From memory the only time you can change the settings of the ORM is in the psuedo constructor of the Application.cfc

I did actually ask / raise with Adobe if there is anyway, this could be change via code. That was back in the days of ColdFusion 9 I think, but yet to hear if anyone else requires it. But yeah if Adobe could allow that, where a ORMRestart could follow it would be magnificent!

But as with anything Adobe related and ColdFusion, my suggestions for the language usually end up being NOT ENOUGH TIME and they close the tickets to never see or hear from them again.

I have tried it already about 1 week ago and unfortunately it HAS to be set in the pseudo constructor. So unless we go about loading the framework in the pseudo constructor and change our internal locking mechanisms, this is not possible.

I wish you could affect it via the onrequestStart() or applicaitonStart(), but as it exists, you can’t unless it is direct in the pseudo constructor

thanks Don. I did make changes inline with your recommendation in the App.cfc

I set a default datasource in the Application’s main body. then onApplicationStart() after i’ve figured out which datasource to use, set the application.datasource. and then do a ORMReload(), followed by super.onApplicationStart() to kickoff coldbox application.

Now…will i be incurring extra application overhead by initializing ORM twice? If it’s only a hit when application starts, I don’t think it would be a big deal.

Yes, I was just thinking about this the other day. I wish Application.cfc had a method that executed BEFORE stuff like ORM loaded up. That would give frameworks that want to influence settings a chance to do so.

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

heh looks like i picked a doozie of an app to learn Coldbox and ORM on :slight_smile:

just to be sure. fwreinit does not run ormreload(), right?