java - Spring Boot + Hibernate 没有为多个数据源使用正确的方言
问题描述
我正在设置一个 Spring Boot (2.1.6)(带有 Spring-Data-Jpa)后端,它使用 2 个不同类型的数据源 - Microsoft SQLServer 和 MySql。即使在配置实体管理器时指定,它也不使用正确的方言。
在启动时,Hibernate 尝试不使用任何方言连接到第一个数据源,即使我指定了一个方言。无法连接后,它会尝试再次连接到同一个数据源 - 这次使用正确的方言。
对于 MySQL,立即使用正确的方言。
MSSQL-配置:
@Configuration
@EnableJpaRepositories(
entityManagerFactoryRef = "gameserverEntityManagerFactory",
transactionManagerRef = "gameserverTransactionManager",
basePackages = { "com.me.repository.mssql" }
)
public class GameserverDataSourceConfig {
@Value("${gameserver.jpa.properties.hibernate.dialect}")
private String hibernateDialect;
...
@Bean(name = "gameserverDataSource")
@ConfigurationProperties(prefix = "gameserver.datasource")
public DataSource gameserverDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setDriverClassName(driverClassName);
return dataSource;
}
@Bean(name = "gameserverEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean
gameserverEntityManagerFactory(EntityManagerFactoryBuilder builder,
@Qualifier("gameserverDataSource") DataSource dataSource) {
HashMap<String, Object> props = new HashMap<>();
props.put("hibernate.dialect", hibernateDialect);
return builder
.dataSource(dataSource)
.properties(props)
.packages("com.me.domain.mssql")
.persistenceUnit("gameDB")
.build();
}
...
MySQL 配置与此类似。
应用程序属性:
#MSSQL
#also tried: jdbc:sqlserver://localhost:49170;databaseName=xyz
gameserver.datasource.url=jdbc:sqlserver://localhost\\xyz:49170
gameserver.datasource.username=root
gameserver.datasource.password=root
gameserver.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
gameserver.jpa.properties.hibernate.dialect=org.hibernate.dialect.SQLServerDialect
#MySQL
webserver.datasource.url=jdbc:mysql://localhost:3306/zyx?useSSL=false&serverTimezone=Europe/Berlin
webserver.datasource.username=root
webserver.datasource.password=root
webserver.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
webserver.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
...
日志输出:
2019-07-15 12:48:06.692 INFO 7200 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [
name: gameDB
...]
2019-07-15 12:48:06.850 INFO 7200 --- [ main] org.hibernate.Version : HHH000412: Hibernate Core {5.3.10.Final}
2019-07-15 12:48:06.858 INFO 7200 --- [ main] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found
2019-07-15 12:48:07.190 INFO 7200 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.0.4.Final}
2019-07-15 12:48:21.859 WARN 7200 --- [ main] o.h.e.j.e.i.JdbcEnvironmentInitiator : HHH000342: Could not obtain connection to query metadata : The TCP/IP connection to the host localhost, port 49170 has failed. Error: "Connection refused: connect. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
2019-07-15 12:48:21.902 INFO 7200 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.SQLServerDialect
2019-07-15 12:48:21.957 INFO 7200 --- [ main] o.h.e.j.e.i.LobCreatorBuilderImpl : HHH000422: Disabling contextual LOB creation as connection was null
2019-07-15 12:48:23.488 INFO 7200 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'gameDB'
2019-07-15 12:48:37.749 INFO 7200 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [
name: webDB
...]
2019-07-15 12:48:37.818 INFO 7200 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
2019-07-15 12:48:38.171 INFO 7200 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'webDB'
解决方案
我自己想通了。我使用的端口与我的仅 SQLServer .properties 中的端口不同...哎呀。
推荐阅读
- javascript - 使用ajax请求时如何删除cors错误?
- java - java - 如何在java hibernate中的单引号之间将参数设置为本机查询?
- ethereum - Solidity 和 Cryptozombies 中的继承:如何部署完整的 OOP 树?
- angular - 在 Angular 2+ 中压缩传出请求
- ios - 如何从 Crashlytics 解释 iOS 崩溃日志?
- c# - 运行时加载 Prism 动态模块
- javascript - 如何从下拉选择中禁用一个选项?
- node.js - 请在 Angular 6 中添加 @Pipe/@Directive/@Component 注释
- c# - 返回位于特定路径的文件
- matlab - 以编程方式返回所有函数的列表