Announcement Announcement Module
Collapse
No announcement yet.
Can Spring use my dataSources? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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">
    </bean>

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

  • #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.
    http://www.springframework.org/docs/...ceAdapter.html

    Comment


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

      Comment


      • #4
        Have you had a read of this?
        http://blog.interface21.com/main/200...ource-routing/

        Comment


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

          Comment


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

            Comment


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

              Comment

              Working...
              X