Vikas Thakur
Vikas Thakur

Reputation: 27

Configure multiple database in spring mvc

We require an application in spring MVC which can interact with multiple databases at a time. In my application-context.xml, I have configured both of the data-sources with different id. Here is my application context file:

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource" />
</bean>


<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
</bean>

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="${db.jndiName}"/>
</bean>

<bean id="jdbcTemplateSec" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSourceSec" />
</bean>

<bean id="dataSourceSec" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="${db.jndiNameSec}"/>
</bean>

But through this I am unable to join multiple tables of different schemas. As each jdbcTemplate has specific datasource which restricts it to access another datasource. So please suggest some solution.

Thanks in advance...

Upvotes: 1

Views: 2787

Answers (1)

Pradeep Kr Kaushal
Pradeep Kr Kaushal

Reputation: 1546

Instead of doing it at java code programming level. You can move the configuration to database. You can attach another database with your existing database provided you have enough permission to do so. And instead of having two data source configuration in your spring you can have only one. Using the JdbcTemplate you can manipulate or access data on two different database.

Here are the links for your reference how to attach one database with another database.
Selecting data from two different servers in SQL Server
Querying data by joining two tables in two database on different servers
Joining tables from different servers

Upvotes: 1

Related Questions