ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From John Moore <j...@jmsd.co.uk>
Subject Re: multiple database/datasources
Date Wed, 31 May 2006 10:11:32 GMT
Denis Vladimirov wrote:

> Another option is to use Spring:
>
> ----------in Spring Config------------
>
>    <bean id="database1DataSource"
>        class="org.springframework.jndi.JndiObjectFactoryBean">
>        <property 
> name="jndiName"><value>java:comp/env/jdbc/database1DS</value>
>            </property>
>    </bean>
>
>    <bean id="database2DataSource"
>        class="org.springframework.jndi.JndiObjectFactoryBean">
>        <property 
> name="jndiName"><value>java:comp/env/jdbc/database2DS</value>
>            </property>
>    </bean>
>
>    <bean id="sqlMapClient"
>        class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
>        <property name="configLocation">
>            <value>/yourPath/ibatisSqlMapConfig.xml</value></property>
>    </bean>
>
>    <bean id="yourDao1"
>        class="com.yourpackage.dao.SameDao">
>        <property name="dataSource"><ref
> local="database1DataSource"/></property>
>        <property name="sqlMapClient"><ref 
> local="sqlMapClient"/></property>
>    </bean>
>
>    <bean id="yourDao2"
>        class="com.yourpackage.dao.SameDao">
>        <property name="dataSource"><ref
> local="database2DataSource"/></property>
>        <property name="sqlMapClient"><ref 
> local="sqlMapClient"/></property>
>    </bean>
>
> <bean id="yourDao3"
>        class="com.yourpackage.dao.AnotherDao">
>        <property name="dataSource"><ref
> local="database2DataSource"/></property>
>        <property name="sqlMapClient"><ref 
> local="sqlMapClient"/></property>
>    </bean>
>
> -----------in Java ---------
> /* the first 2 DAOs are the same but with the different datasource */
>  SameDao yourDaoDB1 = (SameDao ) new ClassPathXmlApplicationContext(
>                "your_spring_config.xml").getBean("yourDao1");
>
>  SameDao yourDaoDB2 = (SameDao ) new ClassPathXmlApplicationContext(
>                "your_spring_config.xml").getBean("yourDao2");
>
>
> AnotherDao anotherDaoDB2 = (AnotherDao) new 
> ClassPathXmlApplicationContext(
>                "your_spring_config.xml").getBean("yourDao3");
>
> Regards,
> Denis
>
>
> On 5/31/06, Marrs, Kasey <Kasey.Marrs@williams.com> wrote:
>
>>
>>
>>
>> I have an application that uses multiple databases to pull data 
>> from.  How
>> do I configure the SQL map config file and the DAO configuration to 
>> access
>> multiple databases?
>>
>> Thanks
>>
>> <>< <>< <>< <>< <><
>> Kasey Marrs
>> 918-573-2242
>> WIT Consulting Company
>> ><> ><> ><> ><> ><>
>>
>
>
There is a potential problem with this approach, in that you are using 
the same mapping files for the different databases, which means that the 
SQL you use has to work for all of them. This is, of course, fine if 
they're all the same type, and even if they're not, you may be able to 
get all you want from using ANSI SQL which all the dbs support. There 
may be advantages, though, in doing something like this:

    <bean id="mysqlDataSource" 
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property 
name="driverClassName"><value>com.mysql.jdbc.Driver</value></property>
        <property 
name="url"><value>jdbc:mysql://localhost:3306/mydb</value></property>
        <property name="username"><value>username</value></property>
        <property name="password"><value>password</value></property>
    </bean>

    <bean id="firebirdDataSource" 
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property 
name="driverClassName"><value>org.firebirdsql.jdbc.FBDriver</value></property>
        <property 
name="url"><value>jdbc:firebirdsql:localhost/3050:D:/Firebird_1_5/databases/mydb.fdb</value></property>
        <property name="username"><value>username</value></property>
        <property name="password"><value>password</value></property>
    </bean>

 
    <bean id="mysqlSqlMapClient" 
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
        <property name="configLocation" value="mysql-sql-map-config.xml"/>
    </bean>

    <bean id="firebirdSqlMapClient" 
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
        <property name="configLocation" 
value="firebird-sql-map-config.xml"/>
    </bean>
      
    <bean id="mysqlDao" class="mypackage.MySQLMapClientDao">
        <property name="dataSource" ref="mysqlDataSource"/>
        <property name="sqlMapClient" ref="mysqlSqlMapClient"/>
    </bean>

    <bean id="firebirdDao" class="mypackage.FirebirdSqlMapClientDao">
        <property name="dataSource" ref="firebirdDataSource"/>
        <property name="sqlMapClient" ref="firebirdSqlMapClient"/>
    </bean>

You set up completely separate mapping files, pointed to by 
"mysql-sql-map-config.xml" and "firebird-sql-map-config.xml" 
respectively, allowing for dialect-specific features to be used, and you 
just pick the DAO bean you want to use for each database action.

-- 
==============================================
John Moore  -  Norwich, UK  -  john@jmsd.co.uk
==============================================

Mime
View raw message