【问题标题】:Spring Boot + Hibernate not using correct dialect for multiple datasourcesSpring Boot + Hibernate 没有为多个数据源使用正确的方言
【发布时间】:2019-07-15 16:52:40
【问题描述】:

我正在设置一个 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 配置与此类似。

application.properties:

#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'

【问题讨论】:

    标签: java sql-server spring hibernate jpa


    【解决方案1】:

    自己想通​​了。我使用的端口与我的仅 SQLServer .properties 中的端口不同...哎呀。

    【讨论】:

      【解决方案2】:

      如果您在上下文中定义了 2 个DataSource bean。在编译和部署之前,您可以使用@Primary 指定您希望在应用程序中使用哪一个。

      【讨论】:

      • 我正在这样做:@Primary @Bean(name = "webserverDataSource") @ConfigurationProperties(prefix = "webserver.datasource") public DataSource webserverDataSource() { return DataSourceBuilder.create().build( ); }
      • 您是说如果您删除另一个数据源 bean,它会完全正常运行吗?
      • 我有另一个版本,它只使用 SQLServer 并且没有指定方言,运行良好是的。不过,为此,Spring Boot 会自动配置所有内容。
      猜你喜欢
      • 1970-01-01
      • 2010-10-26
      • 2017-04-01
      • 2023-03-28
      • 2014-07-24
      • 2015-11-18
      • 2021-03-07
      • 2017-06-24
      • 1970-01-01
      相关资源
      最近更新 更多