{"title":"Spring 多数据源配置冲突解决 - 不同数据库方言设置","description":"本文介绍了在 Spring 配置文件中,当使用多个数据源时,如何解决不同数据库方言设置的冲突。通过将 \u0060spring.jpa.properties.hibernate.dialect\u0060 属性分别设置到每个数据源的配置中,可以确保每个数据源使用正确的方言。","keywords":"Spring, 多数据源, 数据库方言, Hibernate, 配置冲突","content":"根据提供的配置文件,可以看到两个数据源的配置中都有以下属性:

  • \u0060spring.jpa.properties.hibernate.dialect\u0060

这个属性设置了 Hibernate 方言。在原始配置中,这个属性被设置了两次,但是只有最后一次生效。为了解决冲突,我们可以将这个属性分别设置到每个数据源的配置中。

修改后的配置如下:

spring.datasource.primary.jdbc-url=jdbc:postgresql://192.168.0.186:5432/postgis_szjg?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=Asia/Shanghai
spring.datasource.primary.driver-class-name=org.postgresql.Driver
spring.datasource.primary.username=postgres
spring.datasource.primary.password=123456
spring.datasource.primary.db-type=com.zaxxer.hikari.HikariDataSource
spring.datasource.primary.initial-size=5
spring.datasource.primary.min-idle=5
spring.datasource.primary.max-active=100
spring.datasource.primary.max-wait=60000
spring.datasource.primary.validation-query=select version()
spring.datasource.primary.time-between-eviction-runs-millis=60000
spring.datasource.primary.min-evictable-idle-time-millis=300000
spring.datasource.primary.test-while-idle=true
spring.datasource.primary.test-on-borrow=true
spring.datasource.primary.test-on-return=false
spring.datasource.primary.pool-prepared-statements=false
spring.datasource.primary.max-pool-prepared-statement-per-connection-size=20
spring.jpa.properties.primary.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect


spring.datasource.second.jdbc-url=jdbc:oracle:thin:@192.168.0.231:1521:ORCL
spring.datasource.second.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.second.username=KLMY_DZ
spring.datasource.second.password=KLMY_DZ_2023
spring.datasource.second.db-type=com.zaxxer.hikari.HikariDataSource
spring.jpa.properties.second.hibernate.dialect=org.hibernate.dialect.OracleDialect

修改后的配置中,我们将 \u0060spring.jpa.properties.hibernate.dialect\u0060 属性改为了 \u0060spring.jpa.properties.primary.hibernate.dialect\u0060 和 \u0060spring.jpa.properties.second.hibernate.dialect\u0060,以分别对应每个数据源的 Hibernate 方言设置。

Spring 多数据源配置冲突解决 - 不同数据库方言设置

原文地址: https://www.cveoy.top/t/topic/p9lm 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录