Monika Tiwari
Monika Tiwari

Reputation: 151

Multiple DataSource Switching problem in Spring MVC. when the 2nd dataSource is unavailable it automatically uses 1st datasource where 2nd is referred

I have configured a 2 dataSources in my SpringMVC Project, but when the 2nd dataSource is unavailable it automatically uses 1st datasource where 2nd is referred. I want to stop this switching.

Here is the code:

dispatcher-servlet.xml:

<bean id="transactionManager"  class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
</bean>
<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource" lazy-init="false">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="defaultAutoCommit" value="true"/>
        <property name="testOnBorrow" value="true"/>
        <property name="initialSize" value="1"/>
        <property name="maxActive" value="1"/>
        <property name="maxWait" value="500"/>
        <property name="maxIdle" value="2"/>
</bean> 

<bean id="dataSource1" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource" lazy-init="false">
        <property name="driverClassName" value="${jdbc.driverClassName1}"/>
        <property name="url" value="${jdbc.url1}"/>
        <property name="username" value="${jdbc.username1}"/>
        <property name="password" value="${jdbc.password1}"/>
        <property name="defaultAutoCommit" value="true"/>
        <property name="testOnBorrow" value="true"/>
        <property name="initialSize" value="1"/>
        <property name="maxActive" value="3"/>
        <property name="maxWait" value="500"/>
        <property name="maxIdle" value="8"/> 
</bean>

BaseNamedParameterJdbcDaoSupport.java class:

    public class BaseNamedParameterJdbcDaoSupport extends NamedParameterJdbcDaoSupport{

    @Autowired
    public void setDataSourceFor1(DataSource dataSource) {        
//      System.out.println("Main DS"+dataSource);
        setDataSource(dataSource); 
    }

}

BaseNamedParameterJdbcDaoSupportForMirrorDB.java :

public class BaseNamedParameterJdbcDaoSupportForMirrorDB extends NamedParameterJdbcDaoSupport{

    @Autowired
    public void setDataSourceFor2(DataSource dataSource1) {  
//      System.out.println("ForMirrorDB dataSource1"+dataSource1); 
        setDataSource(dataSource1); 
    }

}

Upvotes: 1

Views: 165

Answers (1)

Kunal Kishor
Kunal Kishor

Reputation: 21

It is not possible that the DataSource which are maintained with different name will automatically switch. I think you have implemented some other logic to work with these two DataSource objects.

Check your DAO/Service code to utilizing the "BaseNamedParameterJdbcDaoSupport" & "BaseNamedParameterJdbcDaoSupportForMirrorDB" with proper @Autowired.

Upvotes: 1

Related Questions