6

How do you configure ColdFusion 9's ORM to use multiple DSNs if possible?

Is is possible to setup the datasource in the context of a session scope instead of the application scope?

Or how, in CF9, do you configure Hibernate to use multiple DSNs?


Looks like I should be more specific... I'm looking for a solution that allows for specifying a DSN based on the session.

Here's the scenario. We have a single custom built application that uses multiple DSNs which are determined from the sub-domain. So someone accessing from http://abc.domain.com would use the abc DSN where as someone visiting the xyz.domain.com would use the xyz DSN. The name of the DSN is determined when the session is created and it is stored as a session variable.

I'd like to do something like this:

//Artists.cfc

component persistent="true" datasource="#session.dsn#"
{ 
property name="artistid" generator="increment"; 
property firstname; 
property lastname; 
property address; 
property city; 
property state; 
}

// Application.cfc

component output="false" { 
THIS.name = "MultipleDsnORMTest"; 
THIS.applicationTimeout = createTimeSpan(0, 0, 0, 0); 
THIS.clientManagement = false; 
THIS.datasource = ""; // Leaving black ==> "No data source specified."
                      // Setting to cfbookclub ==> "ORM is not 
                      //   configured for the current application."
                      // Setting to cfartgallery works but doesn't 
                      //   demonstrate use multiple DSNs
THIS.loginStorage = "cookie"; 
THIS.sessionManagement = true; 
THIS.sessionTimeout = createTimeSpan(0, 0, 0, 0); 

THIS.ormenabled = true; 
THIS.ormsettings = {}; 
}
Micah
  • 1,221
  • 17
  • 42

2 Answers2

1

Introduced with the ColdFusion 9.0.1 update, you can use multiple data sources with ORM. One at a time per component. Use the "datasource" attribute in your object, to specify which database should be used.

<cfcomponent displayname="firstObject" datasource="dbOne">
    <cffunction>
        ...
    </cffunction>

    ...
</cfcomponent>

or

component datasource = 'dbOne'{
    ...
}
Micah
  • 1,221
  • 17
  • 42
Andreas Schuldhaus
  • 2,618
  • 2
  • 19
  • 22
  • Wasn't it possible to define a default at the Application.cfc level, meaning you'll only need to add the datasource attribute if you're referencing data from somewhere else? – mz_01 Nov 18 '11 at 06:01
  • If you omit the datasource attribute in the object/component, then the default datasource defined in the application.cfc will be used – Andreas Schuldhaus Nov 18 '11 at 07:04
  • What do you set the Application.cfc level datasource to? – Micah Nov 19 '11 at 00:02
  • Incomplete answer. This doesn't work. Application.cfc has to be configured; the Adobe docs referenced do not show how. – noogrub Apr 16 '15 at 21:55
1

Although it is possible to configure ColdFusion 9 to use multiple data sources with ORM in the application scope, it is not possible to configure ColdFusion 9's ORM to work with multiple DSNs within the session scope.

Micah
  • 1,221
  • 17
  • 42
  • So, did you have to keep `datasource="xyz"` hardcoded (xyz) in your `Artists.cfc`? Or found a way to make it dynamic? – Sergey Galashyn Mar 15 '12 at 19:25
  • @Sergii I have not worked much with CF ORMs because of their limitations on multiple data sources. As much as I hope it not to be true, I believe the answer is that the datasource must be hard-coded. Hopefully someone will correct me if I'm wrong. I regarded hard-coding as that of bad programming practices, so I really hope I'm wrong. – Micah Mar 16 '12 at 15:32