Multiple DataSource Switching problem in Spring MVC. when the 2nd dataSource is unavailable it automatically...
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
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
add a comment |
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
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
add a comment |
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
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
java spring spring-mvc datasource spring-jdbc
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 23 '18 at 7:16
Kunal KishorKunal Kishor
211
211
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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