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

相关版本信息:


春季靴:2.1.6
春季:5.1.8
休眠状态:5.3.10
科特林:1.3.21

最佳答案

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

10-02 00:30
查看更多