[英]How to configure multiple catalogs/databases in sql server which are on same database server in spring boot java 8 app?
In our application we are using spring boot AND sqljdbc4.jar.
在我们的应用程序中,我们使用
spring boot AND sqljdbc4.jar.
We need to connect to different databases(catalogs) inside the same schema (meaning they are in same sql server instance).我们需要连接到同一个模式中的不同数据库(目录)(这意味着它们在同一个 sql 服务器实例中)。 So we need to configure data source in such a way that it can connect to multiple databases and do database operations without issues.
所以我们需要配置数据源,使其可以连接到多个数据库,并且可以毫无问题地进行数据库操作。 But when tried to connect it throws below error
但是当尝试连接时,它会抛出以下错误
invalid object
table_name
found error.发现无效 object
table_name
错误。 // I donot have the complete stack trace but the main error is as mentioned invalid object error.// 我没有完整的堆栈跟踪,但主要错误是前面提到的无效 object 错误。
So we configured two datasources and the source code is as below所以我们配置了两个数据源,源代码如下
Project Structure:项目结构:
src/main/java
- com.foobar
- foo
- domain
- repo
- bar
- domain
- repo
spring.datasource.jdbc-url=jdbc:jdbc:microsoft:sqlserver://HOST:1433;it_foo ```(important note: it_foo will be replaced with qa_foo in different environment which will be managed by profiles)
spring.datasource.username=fooadmin
spring.datasource.password=foo123
spring.datasource.driver-class-name=com.microsoft.jdbc.sqlserver.SQLServerDriver
bar.datasource.jdbc-url=jdbc:jdbc:microsoft:sqlserver://HOST:1433;it_bar(important note: it_foo will be replaced with qa_foo in different environment which will be managed by profiles)
bar.datasource.username=baradmin
bar.datasource.password=bar123
bar.datasource.driver-class-name=com.microsoft.jdbc.sqlserver.SQLServerDriver
Datasource configuration for foo database foo 数据库的数据源配置
package com.foobar;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "entityManagerFactory",
basePackages = { "com.foobar.foo.repo" }
)
public class FooDbConfig {
@Primary
@Bean(name = "dataSource")
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean
entityManagerFactory(
EntityManagerFactoryBuilder builder,
@Qualifier("dataSource") DataSource dataSource
) {
return builder
.dataSource(dataSource)
.packages("com.foobar.foo.domain")
.persistenceUnit("foo")
.build();
}
@Primary
@Bean(name = "transactionManager")
public PlatformTransactionManager transactionManager(
@Qualifier("entityManagerFactory") EntityManagerFactory
entityManagerFactory
) {
return new JpaTransactionManager(entityManagerFactory);
}
}
Datasource configuration for bar bar的数据源配置
med “BarDbConfig.java”
package com.foobar;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "barEntityManagerFactory",
transactionManagerRef = "barTransactionManager",
basePackages = { "com.foobar.bar.repo" }
)
public class BarDbConfig {
@Bean(name = "barDataSource")
@ConfigurationProperties(prefix = "bar.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "barEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean
barEntityManagerFactory(
EntityManagerFactoryBuilder builder,
@Qualifier("barDataSource") DataSource dataSource
) {
return
builder
.dataSource(dataSource)
.packages("com.foobar.bar.domain")
.persistenceUnit("bar")
.build();
}
@Bean(name = "barTransactionManager")
public PlatformTransactionManager barTransactionManager(
@Qualifier("barEntityManagerFactory") EntityManagerFactory
barEntityManagerFactory
) {
return new JpaTransactionManager(barEntityManagerFactory);
}
}
and the overall code is same as in this link: https://medium.com/@joeclever/using-multiple-datasources-with-spring-boot-and-spring-data-6430b00c02e7整体代码与此链接相同: https://medium.com/@joeclever/using-multiple-datasources-with-spring-boot-and-spring-data-6430b00c02e7
I was also going through this links: How to configure Spring boot for work with two databases?我也在浏览这个链接: 如何配置 Spring 引导以使用两个数据库?
Spring boot connect with multiple schemas in mysql Spring 引导与 mysql 中的多个模式连接
I had followed many tutorials to find the optimal solution and went through different posts on stack overflow, but none have a proper solution我遵循了许多教程来找到最佳解决方案,并浏览了有关堆栈溢出的不同帖子,但没有一个合适的解决方案
Also in short, I need to satisfy these requirements.简而言之,我需要满足这些要求。
To figure out optimal solution for this problem so if we need to configure more databases or schemas, the current code should not affect at all.为了找出这个问题的最佳解决方案,所以如果我们需要配置更多的数据库或模式,当前的代码应该完全不受影响。
It should work for different profiles/ environments which has different database names.它应该适用于具有不同数据库名称的不同配置文件/环境。
EDIT: With the above code it works fine but as the databases are in the same database server instance, is there any other optimal solution so as configuring only one datasource?编辑:使用上面的代码它工作正常,但由于数据库在同一个数据库服务器实例中,是否有任何其他最佳解决方案以便只配置一个数据源?
I advise you to add the catalog in each of the instances (entities) of your application.我建议您在应用程序的每个实例(实体)中添加目录。 this way:
这边走:
@Table(name = "test",catalog = "database_test",schema = "dbo")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.