gpt4 book ai didi

java - Spring Boot + Hibernate 未对多个数据源使用正确的方言

转载 作者:行者123 更新时间:2023-12-02 03:02:17 27 4
gpt4 key购买 nike

我正在设置一个 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 中的端口不同...哎呀。

关于java - Spring Boot + Hibernate 未对多个数据源使用正确的方言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57044123/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com