Multi-tenancy Dynamic switch between Datasources is not working
up vote
1
down vote
favorite
I am trying to implement multi-tenancy in my spring boot application. I am referring "http://anakiou.blogspot.com/2015/08/multi-tenant-application-with-spring.html" example. In this example everything is working fine. but in my code Switching between data source is not happening.
In my DataSourceConfig.java I am facing problem to declare 3 Beans, So i am using @Primary annotation. If I remove @Primary annotation I am getting bellow error
' Parameter 1 of method liquibase in com.config.DatabaseConfiguration required a single bean, but 3 were found:
- dataSource: defined by method 'dataSource1' in class path resource [com/config/DataSourceConfig.class]
- dataSource2: defined by method 'dataSource2' in class path resource [com/config/DataSourceConfig.class]
- dataSource3: defined by method 'dataSource3' in class path resource [com/config/DataSourceConfig.class] '
But if I use @Primary annotation irrespective of tenant id its going and storing the data in DataSource annotated with @Primary. So Switching of DataSource is not happening.
This is my DataSourceConfig class
package com.config;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import com.netflix.governator.annotations.binding.Secondary;
@Configuration
public class DataSourceConfig {
@Autowired
private MultitenancyProperties multitenancyProperties;
@Primary
@Bean(name = {"dataSource" , "dataSource1"})
@ConfigurationProperties(prefix = "spring.datasource.datasource1")
public DataSource dataSource1() {
DataSourceBuilder factory = DataSourceBuilder
.create(this.multitenancyProperties.getDatasource1().getClassLoader())
//.driverClassName(this.multitenancyProperties.getDatasource1().getDriverClassName())
.username(this.multitenancyProperties.getDatasource1().getUsername())
.password(this.multitenancyProperties.getDatasource1().getPassword())
.url(this.multitenancyProperties.getDatasource1().getUrl());
return factory.build();
}
@Bean(name = {"dataSource2"})
@ConfigurationProperties(prefix = "spring.datasource.datasource2")
public DataSource dataSource2(@Qualifier("dataSource1") DataSource dataSource1) {
System.out.println("Entered to 2");
DataSourceBuilder factory = DataSourceBuilder
.create(this.multitenancyProperties.getDatasource2().getClassLoader())
//.driverClassName(this.multitenancyProperties.getDatasource2().getDriverClassName())
.username(this.multitenancyProperties.getDatasource2().getUsername())
.password(this.multitenancyProperties.getDatasource2().getPassword())
.url(this.multitenancyProperties.getDatasource2().getUrl());
return factory.build();
}
@Bean(name = {"dataSource3"})
@ConfigurationProperties(prefix = "spring.datasource.datasource3")
public DataSource dataSource3() {
DataSourceBuilder factory = DataSourceBuilder
.create(this.multitenancyProperties.getDatasource3().getClassLoader())
//.driverClassName(this.multitenancyProperties.getDatasource3().getDriverClassName())
.username(this.multitenancyProperties.getDatasource3().getUsername())
.password(this.multitenancyProperties.getDatasource3().getPassword())
.url(this.multitenancyProperties.getDatasource3().getUrl());
return factory.build();
}}
java spring spring-boot multi-tenant spring-config
add a comment |
up vote
1
down vote
favorite
I am trying to implement multi-tenancy in my spring boot application. I am referring "http://anakiou.blogspot.com/2015/08/multi-tenant-application-with-spring.html" example. In this example everything is working fine. but in my code Switching between data source is not happening.
In my DataSourceConfig.java I am facing problem to declare 3 Beans, So i am using @Primary annotation. If I remove @Primary annotation I am getting bellow error
' Parameter 1 of method liquibase in com.config.DatabaseConfiguration required a single bean, but 3 were found:
- dataSource: defined by method 'dataSource1' in class path resource [com/config/DataSourceConfig.class]
- dataSource2: defined by method 'dataSource2' in class path resource [com/config/DataSourceConfig.class]
- dataSource3: defined by method 'dataSource3' in class path resource [com/config/DataSourceConfig.class] '
But if I use @Primary annotation irrespective of tenant id its going and storing the data in DataSource annotated with @Primary. So Switching of DataSource is not happening.
This is my DataSourceConfig class
package com.config;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import com.netflix.governator.annotations.binding.Secondary;
@Configuration
public class DataSourceConfig {
@Autowired
private MultitenancyProperties multitenancyProperties;
@Primary
@Bean(name = {"dataSource" , "dataSource1"})
@ConfigurationProperties(prefix = "spring.datasource.datasource1")
public DataSource dataSource1() {
DataSourceBuilder factory = DataSourceBuilder
.create(this.multitenancyProperties.getDatasource1().getClassLoader())
//.driverClassName(this.multitenancyProperties.getDatasource1().getDriverClassName())
.username(this.multitenancyProperties.getDatasource1().getUsername())
.password(this.multitenancyProperties.getDatasource1().getPassword())
.url(this.multitenancyProperties.getDatasource1().getUrl());
return factory.build();
}
@Bean(name = {"dataSource2"})
@ConfigurationProperties(prefix = "spring.datasource.datasource2")
public DataSource dataSource2(@Qualifier("dataSource1") DataSource dataSource1) {
System.out.println("Entered to 2");
DataSourceBuilder factory = DataSourceBuilder
.create(this.multitenancyProperties.getDatasource2().getClassLoader())
//.driverClassName(this.multitenancyProperties.getDatasource2().getDriverClassName())
.username(this.multitenancyProperties.getDatasource2().getUsername())
.password(this.multitenancyProperties.getDatasource2().getPassword())
.url(this.multitenancyProperties.getDatasource2().getUrl());
return factory.build();
}
@Bean(name = {"dataSource3"})
@ConfigurationProperties(prefix = "spring.datasource.datasource3")
public DataSource dataSource3() {
DataSourceBuilder factory = DataSourceBuilder
.create(this.multitenancyProperties.getDatasource3().getClassLoader())
//.driverClassName(this.multitenancyProperties.getDatasource3().getDriverClassName())
.username(this.multitenancyProperties.getDatasource3().getUsername())
.password(this.multitenancyProperties.getDatasource3().getPassword())
.url(this.multitenancyProperties.getDatasource3().getUrl());
return factory.build();
}}
java spring spring-boot multi-tenant spring-config
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am trying to implement multi-tenancy in my spring boot application. I am referring "http://anakiou.blogspot.com/2015/08/multi-tenant-application-with-spring.html" example. In this example everything is working fine. but in my code Switching between data source is not happening.
In my DataSourceConfig.java I am facing problem to declare 3 Beans, So i am using @Primary annotation. If I remove @Primary annotation I am getting bellow error
' Parameter 1 of method liquibase in com.config.DatabaseConfiguration required a single bean, but 3 were found:
- dataSource: defined by method 'dataSource1' in class path resource [com/config/DataSourceConfig.class]
- dataSource2: defined by method 'dataSource2' in class path resource [com/config/DataSourceConfig.class]
- dataSource3: defined by method 'dataSource3' in class path resource [com/config/DataSourceConfig.class] '
But if I use @Primary annotation irrespective of tenant id its going and storing the data in DataSource annotated with @Primary. So Switching of DataSource is not happening.
This is my DataSourceConfig class
package com.config;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import com.netflix.governator.annotations.binding.Secondary;
@Configuration
public class DataSourceConfig {
@Autowired
private MultitenancyProperties multitenancyProperties;
@Primary
@Bean(name = {"dataSource" , "dataSource1"})
@ConfigurationProperties(prefix = "spring.datasource.datasource1")
public DataSource dataSource1() {
DataSourceBuilder factory = DataSourceBuilder
.create(this.multitenancyProperties.getDatasource1().getClassLoader())
//.driverClassName(this.multitenancyProperties.getDatasource1().getDriverClassName())
.username(this.multitenancyProperties.getDatasource1().getUsername())
.password(this.multitenancyProperties.getDatasource1().getPassword())
.url(this.multitenancyProperties.getDatasource1().getUrl());
return factory.build();
}
@Bean(name = {"dataSource2"})
@ConfigurationProperties(prefix = "spring.datasource.datasource2")
public DataSource dataSource2(@Qualifier("dataSource1") DataSource dataSource1) {
System.out.println("Entered to 2");
DataSourceBuilder factory = DataSourceBuilder
.create(this.multitenancyProperties.getDatasource2().getClassLoader())
//.driverClassName(this.multitenancyProperties.getDatasource2().getDriverClassName())
.username(this.multitenancyProperties.getDatasource2().getUsername())
.password(this.multitenancyProperties.getDatasource2().getPassword())
.url(this.multitenancyProperties.getDatasource2().getUrl());
return factory.build();
}
@Bean(name = {"dataSource3"})
@ConfigurationProperties(prefix = "spring.datasource.datasource3")
public DataSource dataSource3() {
DataSourceBuilder factory = DataSourceBuilder
.create(this.multitenancyProperties.getDatasource3().getClassLoader())
//.driverClassName(this.multitenancyProperties.getDatasource3().getDriverClassName())
.username(this.multitenancyProperties.getDatasource3().getUsername())
.password(this.multitenancyProperties.getDatasource3().getPassword())
.url(this.multitenancyProperties.getDatasource3().getUrl());
return factory.build();
}}
java spring spring-boot multi-tenant spring-config
I am trying to implement multi-tenancy in my spring boot application. I am referring "http://anakiou.blogspot.com/2015/08/multi-tenant-application-with-spring.html" example. In this example everything is working fine. but in my code Switching between data source is not happening.
In my DataSourceConfig.java I am facing problem to declare 3 Beans, So i am using @Primary annotation. If I remove @Primary annotation I am getting bellow error
' Parameter 1 of method liquibase in com.config.DatabaseConfiguration required a single bean, but 3 were found:
- dataSource: defined by method 'dataSource1' in class path resource [com/config/DataSourceConfig.class]
- dataSource2: defined by method 'dataSource2' in class path resource [com/config/DataSourceConfig.class]
- dataSource3: defined by method 'dataSource3' in class path resource [com/config/DataSourceConfig.class] '
But if I use @Primary annotation irrespective of tenant id its going and storing the data in DataSource annotated with @Primary. So Switching of DataSource is not happening.
This is my DataSourceConfig class
package com.config;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import com.netflix.governator.annotations.binding.Secondary;
@Configuration
public class DataSourceConfig {
@Autowired
private MultitenancyProperties multitenancyProperties;
@Primary
@Bean(name = {"dataSource" , "dataSource1"})
@ConfigurationProperties(prefix = "spring.datasource.datasource1")
public DataSource dataSource1() {
DataSourceBuilder factory = DataSourceBuilder
.create(this.multitenancyProperties.getDatasource1().getClassLoader())
//.driverClassName(this.multitenancyProperties.getDatasource1().getDriverClassName())
.username(this.multitenancyProperties.getDatasource1().getUsername())
.password(this.multitenancyProperties.getDatasource1().getPassword())
.url(this.multitenancyProperties.getDatasource1().getUrl());
return factory.build();
}
@Bean(name = {"dataSource2"})
@ConfigurationProperties(prefix = "spring.datasource.datasource2")
public DataSource dataSource2(@Qualifier("dataSource1") DataSource dataSource1) {
System.out.println("Entered to 2");
DataSourceBuilder factory = DataSourceBuilder
.create(this.multitenancyProperties.getDatasource2().getClassLoader())
//.driverClassName(this.multitenancyProperties.getDatasource2().getDriverClassName())
.username(this.multitenancyProperties.getDatasource2().getUsername())
.password(this.multitenancyProperties.getDatasource2().getPassword())
.url(this.multitenancyProperties.getDatasource2().getUrl());
return factory.build();
}
@Bean(name = {"dataSource3"})
@ConfigurationProperties(prefix = "spring.datasource.datasource3")
public DataSource dataSource3() {
DataSourceBuilder factory = DataSourceBuilder
.create(this.multitenancyProperties.getDatasource3().getClassLoader())
//.driverClassName(this.multitenancyProperties.getDatasource3().getDriverClassName())
.username(this.multitenancyProperties.getDatasource3().getUsername())
.password(this.multitenancyProperties.getDatasource3().getPassword())
.url(this.multitenancyProperties.getDatasource3().getUrl());
return factory.build();
}}
java spring spring-boot multi-tenant spring-config
java spring spring-boot multi-tenant spring-config
edited Nov 22 at 7:41
asked Nov 22 at 7:32
Pavan
468
468
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
Disable Spring's DataSourceAutoConfiguration and provide our multi-tenant DataSourceConfig. This will load the properties from MultitenancyProperties which in turn has been configured by application.properties and configure our datasources accordingly. To exclude DataSourceAutoConfiguration use @EnableAutoConfiguration
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
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%2f53425899%2fmulti-tenancy-dynamic-switch-between-datasources-is-not-working%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
up vote
1
down vote
accepted
Disable Spring's DataSourceAutoConfiguration and provide our multi-tenant DataSourceConfig. This will load the properties from MultitenancyProperties which in turn has been configured by application.properties and configure our datasources accordingly. To exclude DataSourceAutoConfiguration use @EnableAutoConfiguration
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
add a comment |
up vote
1
down vote
accepted
Disable Spring's DataSourceAutoConfiguration and provide our multi-tenant DataSourceConfig. This will load the properties from MultitenancyProperties which in turn has been configured by application.properties and configure our datasources accordingly. To exclude DataSourceAutoConfiguration use @EnableAutoConfiguration
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Disable Spring's DataSourceAutoConfiguration and provide our multi-tenant DataSourceConfig. This will load the properties from MultitenancyProperties which in turn has been configured by application.properties and configure our datasources accordingly. To exclude DataSourceAutoConfiguration use @EnableAutoConfiguration
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
Disable Spring's DataSourceAutoConfiguration and provide our multi-tenant DataSourceConfig. This will load the properties from MultitenancyProperties which in turn has been configured by application.properties and configure our datasources accordingly. To exclude DataSourceAutoConfiguration use @EnableAutoConfiguration
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
answered Nov 27 at 9:49
Pavan
468
468
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53425899%2fmulti-tenancy-dynamic-switch-between-datasources-is-not-working%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