Multiple DataSource Switching problem in Spring MVC. when the 2nd dataSource is unavailable it automatically...

Multi tool use
Multi tool use





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















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);
}

}









share|improve this question




















  • 1





    How do you call datasource2 in code?

    – user7294900
    Nov 22 '18 at 7:47













  • like this: @Repository public class DashboardMirrorDAOImpl extends BaseNamedParameterJdbcDaoSupportForMirrorDB implements DashboardMirrorDAO{ , now all the queries inside this Impl will run with 2nd. . but when 2nd is unavailablel or i remove 2nd datasource entry from dispatcher it automaticly uses 1st dataSource :(

    – Monika Tiwari
    Nov 22 '18 at 7:48




















0















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);
}

}









share|improve this question




















  • 1





    How do you call datasource2 in code?

    – user7294900
    Nov 22 '18 at 7:47













  • like this: @Repository public class DashboardMirrorDAOImpl extends BaseNamedParameterJdbcDaoSupportForMirrorDB implements DashboardMirrorDAO{ , now all the queries inside this Impl will run with 2nd. . but when 2nd is unavailablel or i remove 2nd datasource entry from dispatcher it automaticly uses 1st dataSource :(

    – Monika Tiwari
    Nov 22 '18 at 7:48
















0












0








0








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);
}

}









share|improve this question
















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);
}

}






java spring spring-mvc datasource spring-jdbc






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 8:51







Monika Tiwari

















asked Nov 22 '18 at 7:45









Monika TiwariMonika Tiwari

3129




3129








  • 1





    How do you call datasource2 in code?

    – user7294900
    Nov 22 '18 at 7:47













  • like this: @Repository public class DashboardMirrorDAOImpl extends BaseNamedParameterJdbcDaoSupportForMirrorDB implements DashboardMirrorDAO{ , now all the queries inside this Impl will run with 2nd. . but when 2nd is unavailablel or i remove 2nd datasource entry from dispatcher it automaticly uses 1st dataSource :(

    – Monika Tiwari
    Nov 22 '18 at 7:48
















  • 1





    How do you call datasource2 in code?

    – user7294900
    Nov 22 '18 at 7:47













  • like this: @Repository public class DashboardMirrorDAOImpl extends BaseNamedParameterJdbcDaoSupportForMirrorDB implements DashboardMirrorDAO{ , now all the queries inside this Impl will run with 2nd. . but when 2nd is unavailablel or i remove 2nd datasource entry from dispatcher it automaticly uses 1st dataSource :(

    – Monika Tiwari
    Nov 22 '18 at 7:48










1




1





How do you call datasource2 in code?

– user7294900
Nov 22 '18 at 7:47







How do you call datasource2 in code?

– user7294900
Nov 22 '18 at 7:47















like this: @Repository public class DashboardMirrorDAOImpl extends BaseNamedParameterJdbcDaoSupportForMirrorDB implements DashboardMirrorDAO{ , now all the queries inside this Impl will run with 2nd. . but when 2nd is unavailablel or i remove 2nd datasource entry from dispatcher it automaticly uses 1st dataSource :(

– Monika Tiwari
Nov 22 '18 at 7:48







like this: @Repository public class DashboardMirrorDAOImpl extends BaseNamedParameterJdbcDaoSupportForMirrorDB implements DashboardMirrorDAO{ , now all the queries inside this Impl will run with 2nd. . but when 2nd is unavailablel or i remove 2nd datasource entry from dispatcher it automaticly uses 1st dataSource :(

– Monika Tiwari
Nov 22 '18 at 7:48














1 Answer
1






active

oldest

votes


















1














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.






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53426087%2fmultiple-datasource-switching-problem-in-spring-mvc-when-the-2nd-datasource-is%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    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.






    share|improve this answer




























      1














      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.






      share|improve this answer


























        1












        1








        1







        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.






        share|improve this answer













        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 7:16









        Kunal KishorKunal Kishor

        211




        211
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53426087%2fmultiple-datasource-switching-problem-in-spring-mvc-when-the-2nd-datasource-is%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            V qu5QiUS51hMQEPFEniZysb USXZ2nbJl7dddO3n MzM,Dd t0SOp57ifvRJzeGta,gLNr cPU6P5f9 MdzB,b0Wv8 1Gr
            PES6U2KzCo7Gl7QF,5M,2aYdGquvNNCEs,R7 7llY9eM903O,U URDccVADG7VIFl f,U,0L,bfIdhqzJXnQjIBF5

            Popular posts from this blog

            mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

            Multiple Hosts connection in Hyperledger Fabric

            How to send String Array data to Server using php in android