-
Jun 4th, 2007, 01:12 PM
#1
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
-
Jun 4th, 2007, 01:22 PM
#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
-
Jun 4th, 2007, 01:42 PM
#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.
-
Jun 4th, 2007, 01:44 PM
#4
-
Jun 4th, 2007, 01:54 PM
#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
-
Jun 4th, 2007, 01:59 PM
#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.
-
Jun 4th, 2007, 03:38 PM
#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.
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
-
Forum Rules