Use datasource depending on environment?

Hi,
I want to set my DSN dynamically, depending on the environment. have
environment.xml, defined several environments (local, prod etc) and
when I am in a local environment I want to use DSN A, in production
DSN B. Therefore in each <environment> block I have a setting

<Setting name="datasource" value="A" /> where the value for the
datasource is appropriate for that environment.

I also use Transfer and load it using the Transferloader.
Transferloader needs to have the DSN at initialisation:

<Property name="datasourceAlias">A</Property>

Now when I use Transfer, I don't see how I can tell Transfer to use
another datasource that's based ot the datasource setting that depends
on the environment?

Thanks,
Marc

A. Assuming you have a datasource in coldbox.xml.cfm like the one below…

… you can override the datasource name per environment using a line like the one below in environments.xml.cfm.

B. In the transfer interceptor declaration, use the line below to reference the datasource alias and it should all fall into place per environment.

globalDSN

  • Gabriel

Thanks Gabriel,

I changed te lines in coldbox.xml and for each environment in
environment.xml. I now get a message

"The setting datasource does not exist.
FWSetting flag is false "

It occurs here (the line with the ==> <=='s):

<cffunction name="onAppInit" access="public" returntype="void"
output="false">
  <cfargument name="Event" type="any">
==> <cfset getColdboxOCM().set("utils",getModel("utils"))> <==
...

function onAppInit is my <Setting name="ApplicationStartHandler"
value="main.onAppInit"/>

The next line in the trace shows some inner CB file (D:\www\wwwroot
\SimpleCMS\coldbox\system\frameworkSupertype.cfc) but the function
being called in this line _does_ try to get to the datasource:

<cffunction name="_init" access="public">
  <cfargument name="DSN" type="Any"
_wireme="coldbox:setting:datasources">

But then coldbox:setting:datasources is not a setting datasource.

I want to get to the datasource here to get some data from the DB and
then put it in the Variables scope so it is accessible later on. Not
sure if this is possible at this stage of the request and if it is a
good practice...

How can I solve this?

Thanks,

Marc

OK so I got "The setting datasource does not exist"

Then why not ad a

<Setting name="datasource" value="myDSM" />

for each environment? So I have now for each environement these 2
lines

<Setting name="Datasources.globalDSN.name" value="myDSNNameA" />
<Setting name="datasource" value="myDSNNameB" />

I Request a page from domain A and app uses DSN myDSNNameA, I Request
a page from domain B and app uses DSN myDSNNameB...
Beats me why but I'm glad I got it at least working.

Marc

Marc, I’m glad you have something working for you. But if it breaks or if you have more time to play around with how it works, read this section of the environment interceptor that explains how to override configuration values.

http://ortus.svnrepository.com/coldbox/trac.cgi/wiki/cbEnvironmentControl#environmentelement

In a nutshell, you want to declare your datasource in the configuration file and make sure that it’s working. You’d then override the values for each environment.

  • Gabriel