Q:

create multiple datasource in spring bean.xml

I am using Spring jdbctemplate. Want to create multiple datasource in bean.xml.
Below is the details of how i created a single datasource in bean.xml.
<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />

<property name="username" value="xxx" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="password" value="yyy"/>

<!-- Definition for JDBCTemplate bean -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource"></property>
</bean>

Now i want to add another datasource with the differnt database. how do i do it in bean.xml.
Please do not give me alternatives. First tell whether it is possible or not. If possible how to do it.

A:

defining datasource-1
<bean name="mysqlDatasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/test" />
    <property name="username" value="xxx" />
    <property name="password" value="yyy" />
</bean>

similarly, defining datasource-2
<bean id="oracleDatasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
  <property name="url" value="jdbc:oracle:thin:@localhost:1521:test"/>
  <property name="username" value="oracle_user"/>
  <property name="password" value="oracle_password"/>
</bean>

JdbcTemplate for MySQL
<bean id="mysqlJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="mysqlDatasource"/>
</bean>

JdbcTemplate for Oracle
<bean id="oracleJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="oracleDatasource"/>
</bean>

Hope this clears your query.

