首页 > 解决方案 > 在 Spring Boot 2.1 中覆盖 Hibernate ConnectionProvider

问题描述

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

以前我曾经EntityManagerFactoryBeanCallback将 设置hibernate.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 的变量来启用我们的多租户。任何允许我SET @scope_id = ?在每个连接上运行查询的替代解决方案也可以。

相关版本信息:

标签: javaspringhibernatespring-bootkotlin

解决方案


事实证明,数据源不再正确地注入我的体内ScopedConnectionProvider。我改变了它,以便我继承而不是从头开始DatasourceConnectionProviderImpl实现我自己的。ConnectionProvider这似乎消除了指定方言的需要。


推荐阅读