java - 在 Spring Boot 2.1 中重写 Hibernate ConnectionProvider

标签 java spring hibernate spring-boot kotlin

我正在升级 kotlin Spring Boot 1.x 应用程序以使用 Spring Boot 2.1.6

之前,我使用 EntityManagerFactoryBeanCallbackhibernate.connection.provider_class 设置为 ConnectionProvider 的自定义实现。这已分别在 Spring Boot 2.0 和 2.1 中被弃用并删除。

我现在尝试使用 HibernatePropertiesCustomizer

@Bean
fun hibernatePropertiesCustomizer() = HibernatePropertiesCustomizer {
    it["hibernate.connection.provider_class"] = ScopedConnectionProvider::class.java.name
}

应用程序现在抛出错误:

java.lang.IllegalStateException: Failed to load ApplicationContext

    Caused by:
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]

        Caused by:
        org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]

            Caused by:
            org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set

如果我注释掉 it["hibernate.connection.provider_class"] = ScopedConnectionProvider::class.java.name 行,那么除了自定义 ConnectionProvider 之外,一切都会正常工作

作为引用,我使用 ScopedConnectionProvider 来通过在表示客户范围 ID 的每个连接上设置一个变量来启用 Multi-Tenancy 。任何允许我在每个连接上运行 SET @scope_id = ? 查询的替代解决方案也可以。

相关版本信息:

  • Spring 启动:2.1.6
  • Spring :5.1.8
  • hibernate :5.3.10
  • Kotlin:1.3.21

最佳答案

事实证明,数据源不再正确地注入(inject)到我的 ScopedConnectionProvider 中。我对其进行了更改,以便从 DatasourceConnectionProviderImpl 继承,而不是从头开始实现我自己的 ConnectionProvider。这似乎消除了指定方言的需要。

关于java - 在 Spring Boot 2.1 中重写 Hibernate ConnectionProvider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56998865/

相关文章:

java - Spring 启动: Rest endpoint integration with Kafka

mysql - 在 Hibernate 中实现对同一个表中另一个条目的引用

java - 使用 Freenas 11.2 我的 java 进程在离开 jail shell 时关闭

java - 是否有一个库函数可以将数字舍入到无穷大(即远离 0)

spring - 在 Tomcat 和 Spring 中使用 SSL

java - JPA 和 Hibernate 有什么区别?

java - 两个依赖链 OSGI

java - 如何让方形星星图案看起来像棋盘?

java - 使用maven用不同版本的jdk编译jar

java - Spring 3.2 PropertySourcesPlaceholderConfigurer 与 JBoss EAP 6.2