Configuring the application to use multiple data sources

Configure Persistent CFCs with the attribute datasource pointing to the appropriate data source. You can specify the attribute data source on the CFC using the tag cfcomponent or by specifying the annotation on Component in the CFC definition. If you do not specify a data source, the default data source is used for that CFC.
Since a Hibernate configuration uses a single data source, all related CFCs (using ORM relationships) must have the same data source.

Example

Art.cfc

<cfcomponent persistent="true" datasource="artgallery" table="Art"> 
... 
</cfcomponent>

Author.cfc

<cfcomponent persistent="true" datasource="bookclub" table="author"> 

</cfcomponent>

ORM settings

The following are the data source-specific ORM settings for which you can specify  string  or struct values in the Application.cfc:

  • schema
  • catalog
  • dialect
  • dbcreate
  • sqlscript
    For multiple data sources, a struct can be specified with data source name as the key and the appropriate setting as the value. If a string value is specified, it applies to the default data source of ORM.

Example 1

<cfset this.ormsettings.dbcreate={artgallery="dropcreate", bookclub="none"}>

Example 2

<cfset this.ormsettings.dbcreate="dropcreate"> If multiple data sources are used for ORM, these settings apply to the default ORM data source.

Mapping using Hibernate mapping files

In multiple data source scenarios, the data source information must be provided in the CFC (and not in .hbmxml file). 
Also, all CFCs used in one .hbmxml file must have the same data source.

Example

The following example illustrates two different entities using two different data sources. In this example, art.cfc and artist.cfc are related and therefore use the same data source.art.cfc

<cfcomponent persistent="true" table="art" datasource="cfartgallery"> 
<cfproperty name="ArtID" fieldtype="id" generator="native"> 
<cfproperty name="ArtName"> 
<cfproperty name="IsSold"> 
</cfcomponent>

artists.cfc

<cfcomponent persistent="true" table="artists" datasource="cfartgallery"> 
<cfproperty name="ArtistID" fieldtype="id"> 
<cfproperty name="FirstName"> 
<cfproperty name="LastName"> 
<cfproperty name="art" fieldtype="one-to-many" cfc="art" fkcolumn="ArtistID"> 
</cfcomponent>

authors.cfc

<cfcomponent persistent="true" table=authors datasource="cfbookclub"> 
<cfproperty name="AuthorID" fieldtype="id"> 
<cfproperty name="LastName"> 
<cfproperty name="FirstName"> 
</cfcomponent>

index.cfm

<cfoutput>Original Data<br></cfoutput> 
<cfset artistObj = EntityLoad("artists", 1, true)> 
<cfoutput>#artistObj.getArtistID()# | #artistObj.getFirstName()# | 
#artistObj.getLastName()#<br></cfoutput> 
<cfset artObj = artistObj.getart()> 
<cfoutput>#artObj[1].getartname()# <br></cfoutput> 
<cfset authorObj = EntityLoad("authors", 1, true)> 
<cfoutput>#authorObj.getFirstName()#</cfoutput> 
<cfoutput>#authorObj.getLastName()#</cfoutput>

Get help faster and easier

New user?