gpt4 book ai didi

java - 在 Spring Boot 2.1 中重写 Hibernate ConnectionProvider

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

我正在升级 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/

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