You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-cs@ibatis.apache.org by "Nathan O. Miller" <na...@iifdata.com> on 2008/01/25 19:38:17 UTC

Multiple Datasources?

Hello everyone,

 

I have a web application that contains individual tools that each use their
own database.  Is there some way for IBatis to support the use of multiple
datasources for something like this?  

 

Thanks,

Nathan Miller


Re: Multiple Datasources?

Posted by Vincent Apesa <va...@hotmail.com>.
I think what Nathan meant was if he has a web application which uses multiple databases can he specify the datasource for each Map file.

Vince
  ----- Original Message ----- 
  From: Clough, Samuel (USPC.PRG.Atlanta) 
  To: user-cs@ibatis.apache.org 
  Sent: Friday, January 25, 2008 1:57 PM
  Subject: RE: Multiple Datasources?


  I know isn't much help, but I think you can use multiple datasources by diving into the api a little.



  From: Nathan O. Miller [mailto:nathan.miller@iifdata.com] 
  Sent: Friday, January 25, 2008 1:38 PM
  To: user-cs@ibatis.apache.org
  Subject: Multiple Datasources?



  Hello everyone,



  I have a web application that contains individual tools that each use their own database.  Is there some way for IBatis to support the use of multiple datasources for something like this?  



  Thanks,

  Nathan Miller


------------------------------------------------------------------------------

  Princeton Retirement Group, Inc - Important Terms 

  This E-mail is not intended for distribution to, or use by, any person or entity in any location where such distribution or use would be contrary to law or regulation, or which would subject Princeton Retirement Group, Inc. or any affiliate to any registration requirement within such location. 

  This E-mail may contain privileged or confidential information or may otherwise be protected by work product immunity or other legal rules. No confidentiality or privilege is waived or lost by any mistransmission. Access, copying or re-use of information by non-intended or non-authorized recipients is prohibited. If you are not an intended recipient of this E-mail, please notify the sender, delete it and do not read, act upon, print, disclose, copy, retain or redistribute any portion of this E-mail. 

  The transmission and content of this E-mail cannot be guaranteed to be secure or error-free. Therefore, we cannot represent that the information in this E-mail is complete, accurate, uncorrupted, timely or free of viruses, and Princeton Retirement Group, Inc. cannot accept any liability for E-mails that have been altered in the course of delivery. Princeton Retirement Group, Inc. reserves the right to monitor, review and retain all electronic communications, including E-mail, traveling through its networks and systems (subject to and in accordance with local laws). If any of your details are incorrect or if you no longer wish to receive mailings such as this by E-mail please contact the sender by reply E-mail. 


------------------------------------------------------------------------------

RE: Multiple Datasources?

Posted by "Clough, Samuel (USPC.PRG.Atlanta)" <Sa...@princetonrg.com>.
I know isn't much help, but I think you can use multiple datasources by
diving into the api a little.

 

From: Nathan O. Miller [mailto:nathan.miller@iifdata.com] 
Sent: Friday, January 25, 2008 1:38 PM
To: user-cs@ibatis.apache.org
Subject: Multiple Datasources?

 

Hello everyone,

 

I have a web application that contains individual tools that each use
their own database.  Is there some way for IBatis to support the use of
multiple datasources for something like this?  

 

Thanks,

Nathan Miller 
--------------------------------------------------------

Princeton Retirement Group, Inc - Important Terms 
This E-mail is not intended for distribution to, or use by, any person or entity in any location where such distribution or use would be contrary to law or regulation, or which would subject Princeton Retirement Group, Inc. or any affiliate to any registration requirement within such location. 
This E-mail may contain privileged or confidential information or may otherwise be protected by work product immunity or other legal rules. No confidentiality or privilege is waived or lost by any mistransmission. Access, copying or re-use of information by non-intended or non-authorized recipients is prohibited. If you are not an intended recipient of this E-mail, please notify the sender, delete it and do not read, act upon, print, disclose, copy, retain or redistribute any portion of this E-mail. 
The transmission and content of this E-mail cannot be guaranteed to be secure or error-free. Therefore, we cannot represent that the information in this E-mail is complete, accurate, uncorrupted, timely or free of viruses, and Princeton Retirement Group, Inc. cannot accept any liability for E-mails that have been altered in the course of delivery. Princeton Retirement Group, Inc. reserves the right to monitor, review and retain all electronic communications, including E-mail, traveling through its networks and systems (subject to and in accordance with local laws). If any of your details are incorrect or if you no longer wish to receive mailings such as this by E-mail please contact the sender by reply E-mail. 

--------------------------------------------------------

RE: Multiple Datasources?

Posted by Kamiel Wanrooij <K....@everywebsolutions.nl>.
We have done this by creating an SqlMapClient that handles all access to the Sql Mapper. We created a ProxyFactory that proxies the original DataSource in the SqlMapper. This way, we can resolve the datasource that the mapper uses at runtime. We let IBatis configure the datasource, and only proxy it when we need to.

 

We use Spring.NET to configure the possible proxies, for instance, we want to resolve a different database based on the URL, but our mapper class does not have to know that it is running in an ASP.NET environment. We configure the proxies somewhere else.

 

A code snippet from our SqlMapClient: 

 

 

DomSqlMapBuilder builder = new DomSqlMapBuilder();

mapper = builder.Configure(SqlMapConfig);

mapper.DataSource = dataSourceProxyFactory.GetProxiedDataSource(mapper.DataSource);

 

And our datasource proxyfactory (a generic, so we can use it to instantiate any type of proxy) looks like this:

 

    /// <summary>

    /// Provides a generic way of wrapping IBatis DataSource objects

    /// </summary>

    /// <typeparam name="T">The type of proxy to use</typeparam>

    public class GenericIBatisDataSourceProxyFactory<T> : IDataSourceProxyFactory where T : IDataSourceProxy

    {

        /// <summary>

        /// Creates a new instance of T, assigns the datasource to it, and returns it.

        /// </summary>

        /// <param name="dataSource">The datasource to proxy</param>

        /// <returns>The proxied datasource of type T</returns>

        public IDataSource GetProxiedDataSource(IDataSource dataSource)

        {

            T dataSourceProxy = Activator.CreateInstance<T>();

            dataSourceProxy.DataSource = dataSource;

 

            return dataSourceProxy;

        }

    }

 

And IDataSourceProxy is just a IDataSource that has a setter for the proxied datasource itself, so it basically wraps the calls to the original datasource, changing when necessary.

 

This way every data access objects can use the same Sql Mapper (we use singleton dao’s, so we can’t get a separate instance for each datasource) but we can pick the database at runtime by modifying the connection string in the datasource proxy.

 

Hope this helps.

 

 

Kamiel Wanrooij
EveryWeb Solutions

 

Vinkenstraat 3bg
1013 JL Amsterdam, The Netherlands
T: +31 20 6236445 / +31 614194798
E: k.wanrooij@everywebsolutions.nl <mailto:k.wanrooij@everywebsolutions.nl%20> 
W: http://www.everywebsolutions.nl <http://www.everywebsolutions.nl/> 

 

From: smartkid [mailto:skyaoj@gmail.com] 
Sent: Saturday, January 26, 2008 4:16 PM
To: user-cs@ibatis.apache.org
Subject: 答复: Multiple Datasources?

 

〉〉It sounds like it can't be done in an application that has a base data access class that all other data access implementations inherit from.

 

It depends on what your base data access class looks like~~~~~I suggest your data access class can takes an ISqlMapper instance as a constructor parameter, or better, applies the factory pattern for the creation of the data access classes.

 

>> It would be really nice if the datasource could be set on a map file by map file basis.

I don’t understand what u really mean L

The datasource is set in SqlMap.xml, for a multi database application, you should have multiple SqlMap.xml files, and the datasource element in each SqlMap.xml points to a different database.

 

发件人: Vincent Apesa [mailto:vapesa@hotmail.com] 
发送时间: 2008年1月26日 22:41
收件人: user-cs@ibatis.apache.org; smrtk@hotmail.com
主题: Re: Multiple Datasources?

 

It sounds like it can't be done in an application that has a base data access class that all other data access implementations inherit from.

 

It would be really nice if the datasource could be set on a map file by map file basis.

 

Vince

	----- Original Message ----- 

	From: smartkid <ma...@gmail.com>  

	To: user-cs@ibatis.apache.org 

	Sent: Saturday, January 26, 2008 2:19 AM

	Subject: 答复: Multiple Datasources?

	 

	Hi, 

	 

	1.       Creates a SqlMap.xml and data maps for each database.

	2.       Creates a DomSqlMapBuilder instance for each database.

	3.       Call DomSqlMapBuilder.Configure(…) with the corresponding SqlMap.xml for each instance.

	4.       Use the ISqlMapper instances returned from the previous step for accessing a certain database.

	 

	 

	Pseudo code:

	 

	DomSqlMapBuilder builderA = new DomSqlMapBuilder();

	ISqlMapper mapperA = builderA.Configure(“sqlMap_A.xml”);

	 

	DomSqlMapBuilder builderB = new DomSqlMapBuilder();

	ISqlMapper mapperB = builder.Configure(“sqlMap_B.xml”);

	 

	 

	Smartkid

	 

	发件人: Nathan O. Miller [mailto:nathan.miller@iifdata.com] 
	发送时间: 2008年1月26日 2:38
	收件人: user-cs@ibatis.apache.org
	主题: Multiple Datasources?

	 

	Hello everyone,

	 

	I have a web application that contains individual tools that each use their own database.  Is there some way for IBatis to support the use of multiple datasources for something like this?  

	 

	Thanks,

	Nathan Miller


Re: Multiple Datasources?

Posted by Vincent Apesa <va...@hotmail.com>.
>>The datasource is set in SqlMap.xml, for a multi database application, you should have multiple SqlMap.xml files, and the datasource element in each SqlMap.xml points to a different database.

Right. I'll just change my implementation so the base class takes a SqlMap parameter. I was using a single/ shared ISqlMapper that every DAO shared. It's an easy fix. thanks for the setting me straight :)

Nathan, I think this solves your problem.

Vince
  ----- Original Message ----- 
  From: smartkid 
  To: user-cs@ibatis.apache.org 
  Sent: Saturday, January 26, 2008 10:16 AM
  Subject: 答复: Multiple Datasources?


  〉〉It sounds like it can't be done in an application that has a base data access class that all other data access implementations inherit from.

   

  It depends on what your base data access class looks like~~~~~I suggest your data access class can takes an ISqlMapper instance as a constructor parameter, or better, applies the factory pattern for the creation of the data access classes.

   

  >> It would be really nice if the datasource could be set on a map file by map file basis.

  I don’t understand what u really mean L

  The datasource is set in SqlMap.xml, for a multi database application, you should have multiple SqlMap.xml files, and the datasource element in each SqlMap.xml points to a different database.

   

  发件人: Vincent Apesa [mailto:vapesa@hotmail.com] 
  发送时间: 2008年1月26日 22:41
  收件人: user-cs@ibatis.apache.org; smrtk@hotmail.com
  主题: Re: Multiple Datasources?

   

  It sounds like it can't be done in an application that has a base data access class that all other data access implementations inherit from.

   

  It would be really nice if the datasource could be set on a map file by map file basis.

   

  Vince

    ----- Original Message ----- 

    From: smartkid 

    To: user-cs@ibatis.apache.org 

    Sent: Saturday, January 26, 2008 2:19 AM

    Subject: 答复: Multiple Datasources?

     

    Hi, 

     

    1.       Creates a SqlMap.xml and data maps for each database.

    2.       Creates a DomSqlMapBuilder instance for each database.

    3.       Call DomSqlMapBuilder.Configure(…) with the corresponding SqlMap.xml for each instance.

    4.       Use the ISqlMapper instances returned from the previous step for accessing a certain database.

     

     

    Pseudo code:

     

    DomSqlMapBuilder builderA = new DomSqlMapBuilder();

    ISqlMapper mapperA = builderA.Configure(“sqlMap_A.xml”);

     

    DomSqlMapBuilder builderB = new DomSqlMapBuilder();

    ISqlMapper mapperB = builder.Configure(“sqlMap_B.xml”);

     

     

    Smartkid

     

    发件人: Nathan O. Miller [mailto:nathan.miller@iifdata.com] 
    发送时间: 2008年1月26日 2:38
    收件人: user-cs@ibatis.apache.org
    主题: Multiple Datasources?

     

    Hello everyone,

     

    I have a web application that contains individual tools that each use their own database.  Is there some way for IBatis to support the use of multiple datasources for something like this?  

     

    Thanks,

    Nathan Miller

答复: Multiple Datasources?

Posted by smartkid <sk...@gmail.com>.
〉〉It sounds like it can't be done in an application that has a base data
access class that all other data access implementations inherit from.

 

It depends on what your base data access class looks like~~~~~I suggest your
data access class can takes an ISqlMapper instance as a constructor
parameter, or better, applies the factory pattern for the creation of the
data access classes.

 

>> It would be really nice if the datasource could be set on a map file by
map file basis.

I don’t understand what u really mean L

The datasource is set in SqlMap.xml, for a multi database application, you
should have multiple SqlMap.xml files, and the datasource element in each
SqlMap.xml points to a different database.

 

发件人: Vincent Apesa [mailto:vapesa@hotmail.com] 
发送时间: 2008年1月26日 22:41
收件人: user-cs@ibatis.apache.org; smrtk@hotmail.com
主题: Re: Multiple Datasources?

 

It sounds like it can't be done in an application that has a base data
access class that all other data access implementations inherit from.

 

It would be really nice if the datasource could be set on a map file by map
file basis.

 

Vince

----- Original Message ----- 

From: smartkid <ma...@gmail.com>  

To: user-cs@ibatis.apache.org 

Sent: Saturday, January 26, 2008 2:19 AM

Subject: 答复: Multiple Datasources?

 

Hi, 

 

1.       Creates a SqlMap.xml and data maps for each database.

2.       Creates a DomSqlMapBuilder instance for each database.

3.       Call DomSqlMapBuilder.Configure(…) with the corresponding
SqlMap.xml for each instance.

4.       Use the ISqlMapper instances returned from the previous step for
accessing a certain database.

 

 

Pseudo code:

 

DomSqlMapBuilder builderA = new DomSqlMapBuilder();

ISqlMapper mapperA = builderA.Configure(“sqlMap_A.xml”);

 

DomSqlMapBuilder builderB = new DomSqlMapBuilder();

ISqlMapper mapperB = builder.Configure(“sqlMap_B.xml”);

 

 

Smartkid

 

发件人: Nathan O. Miller [mailto:nathan.miller@iifdata.com] 
发送时间: 2008年1月26日 2:38
收件人: user-cs@ibatis.apache.org
主题: Multiple Datasources?

 

Hello everyone,

 

I have a web application that contains individual tools that each use their
own database.  Is there some way for IBatis to support the use of multiple
datasources for something like this?  

 

Thanks,

Nathan Miller


Re: Multiple Datasources?

Posted by Vincent Apesa <va...@hotmail.com>.
It sounds like it can't be done in an application that has a base data access class that all other data access implementations inherit from.

It would be really nice if the datasource could be set on a map file by map file basis.

Vince
  ----- Original Message ----- 
  From: smartkid 
  To: user-cs@ibatis.apache.org 
  Sent: Saturday, January 26, 2008 2:19 AM
  Subject: 答复: Multiple Datasources?


  Hi, 

   

  1.       Creates a SqlMap.xml and data maps for each database.

  2.       Creates a DomSqlMapBuilder instance for each database.

  3.       Call DomSqlMapBuilder.Configure(…) with the corresponding SqlMap.xml for each instance.

  4.       Use the ISqlMapper instances returned from the previous step for accessing a certain database.

   

   

  Pseudo code:

   

  DomSqlMapBuilder builderA = new DomSqlMapBuilder();

  ISqlMapper mapperA = builderA.Configure(“sqlMap_A.xml”);

   

  DomSqlMapBuilder builderB = new DomSqlMapBuilder();

  ISqlMapper mapperB = builder.Configure(“sqlMap_B.xml”);

   

   

  Smartkid

   

  发件人: Nathan O. Miller [mailto:nathan.miller@iifdata.com] 
  发送时间: 2008年1月26日 2:38
  收件人: user-cs@ibatis.apache.org
  主题: Multiple Datasources?

   

  Hello everyone,

   

  I have a web application that contains individual tools that each use their own database.  Is there some way for IBatis to support the use of multiple datasources for something like this?  

   

  Thanks,

  Nathan Miller

答复: Multiple Datasources?

Posted by smartkid <sk...@gmail.com>.
Hi, 

 

1.       Creates a SqlMap.xml and data maps for each database.

2.       Creates a DomSqlMapBuilder instance for each database.

3.       Call DomSqlMapBuilder.Configure(…) with the corresponding
SqlMap.xml for each instance.

4.       Use the ISqlMapper instances returned from the previous step for
accessing a certain database.

 

 

Pseudo code:

 

DomSqlMapBuilder builderA = new DomSqlMapBuilder();

ISqlMapper mapperA = builderA.Configure(“sqlMap_A.xml”);

 

DomSqlMapBuilder builderB = new DomSqlMapBuilder();

ISqlMapper mapperB = builder.Configure(“sqlMap_B.xml”);

 

 

Smartkid

 

发件人: Nathan O. Miller [mailto:nathan.miller@iifdata.com] 
发送时间: 2008年1月26日 2:38
收件人: user-cs@ibatis.apache.org
主题: Multiple Datasources?

 

Hello everyone,

 

I have a web application that contains individual tools that each use their
own database.  Is there some way for IBatis to support the use of multiple
datasources for something like this?  

 

Thanks,

Nathan Miller