Announcement Announcement Module
No announcement yet.
Can Spring use my dataSources? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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">

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

  • #2
    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.


    • #3
      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 and in the following xml file.

      The end user could config the following xml file.
      <tenant id="test1">
      <tenant id="test2">

      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
        Have you had a read of this?


        • #5
          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 ">

          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

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



          • #6
            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
              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.