Results 1 to 7 of 7

Thread: Can Spring use my dataSources?

  1. #1
    Join Date
    Apr 2007
    Location
    Montreal, Canada
    Posts
    31

    Default Can Spring use my dataSources?

    Hi, all
    I have to get information from different DB; I get user name and password for each DB from different file, so I already create a map of dataSources, such as, Map<String, BasicDataSource> dataSources

    But I found I have to set a DataSource for my transactionManager.

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSou rceTransactionManager">
    </bean>

    Is there a good way to let spring using my dataSources?
    Thanks.
    Arden

  2. #2
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    Any chance you can explain a little more about what you're trying to do? There is an adapter that Spring provides for differing credentials. If you can explain a little more it might help.
    http://www.springframework.org/docs/...ceAdapter.html

  3. #3
    Join Date
    Apr 2007
    Location
    Montreal, Canada
    Posts
    31

    Default

    The application(A) will provide one kind of service to customer.

    Different customer will use different data source.
    These information saved in different properties file, such as test1.properties and test2.properties in the following xml file.

    The end user could config the following xml file.
    <configuration>
    <tenants>
    <tenant id="test1">
    <path>C:\share\test1.properties</path>
    </tenant>
    <tenant id="test2">
    <path>C:\share\test2.properties</path>
    </tenant>
    </tenants>
    </configuration>

    Then the application will run one sql: "select count(*) from ACCOUNT" on each different database, then consolidate the number of account.

    Now, I have loaded the xml file into :
    Map<String, BasicDataSource> dataSources

    so I want to my spring DAO to use my dataSources, then what can I do?

    Thanks a lot.

  4. #4
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

  5. #5
    Join Date
    Apr 2007
    Location
    Montreal, Canada
    Posts
    31

    Default

    Yes. I read it.
    I could have a class as:
    TotalRoutingDataSource extends AbstractRoutingDataSource {

    }

    Then call setTargetDataSources(Map targetDataSources) to ask spring to use my dataSources.

    But if I define a dataSource as:
    <bean id="dataSource"
    class="TotalRoutingDataSource ">
    </bean>

    I will give me the following error message:

    Caused by: java.lang.IllegalArgumentException: targetDataSources is required
    at org.springframework.jdbc.datasource.lookup.Abstrac tRoutingDataSource.afterPropertiesSet(AbstractRout ingDataSource.java:95)

    So I have to define targetDataSources in my xml, but it is only existed in my java code.

    Thanks.
    Arden

  6. #6
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    If you really want to do this in Java then you can simply override afterPropertiesSet(). The problem here I guess is that when afterPropertiesSet fires you haven't set set your dataSources.

  7. #7
    Join Date
    Feb 2005
    Location
    Boston, MA
    Posts
    1,142

    Default

    Take a look at this post. It starts off the same way this one does, but was before AbstractRoutingDataSource, but describes the same technique. It goes on to describe a similar technique to use swapping in multiple DAOs instead of just datasources, since each DAO uses a different DataSource and TransactionManager.
    Bill

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •