Spring Boot Hikari 属性以编程方式覆盖
Spring Boot Hikari properties override programmatically
我的 Spring 启动应用程序中有两个数据源,因此我无法使用标准数据源属性。其中之一定义如下:
vertica:
datasource:
jdbc-url: jdbc:vertica://${DR_HOST}:${DR_PORT}/${DR_DB_NAME}
username: ${DR_USER}
password: ${DR_PASSWORD}
driver-class-name: com.vertica.jdbc.Driver
hikari:
connectionTimeout: 30000
idleTimeout: 10000
maxLifetime: 10000
keepaliveTime: 5000
maximumPoolSize: 20
minimumIdle: 5
#poolName: vertica-db-pool
#username: ${DB_USER}
#password: ${DB_PASSWORD}
jpa:
hibernate:
ddl-auto: none
format_sql: true
show-sql: true
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
naming-strategy: org.hibernate.cfg.ImprovedNamingStrategy
properties:
hibernate:
dialect: org.hibernate.dialect.VerticaDialect
@Configuration
@ConfigurationProperties("vertica.datasource")
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "verticaEntityManagerFactory",
transactionManagerRef = "verticaTransactionManager",
basePackages = { "com.yyy.vertica" }
)
public class MyDataSourceConfig extends HikariConfig {
public final static String PERSISTENCE_UNIT_NAME = "vertica";
public final static String PACKAGES_TO_SCAN = "com.xxx.entity";
@Autowired
private Environment env;
@Bean
public HikariDataSource verticaDataSource() {
return new HikariDataSource(this);
}
@Bean
public LocalContainerEntityManagerFactoryBean verticaEntityManagerFactory(
final HikariDataSource verticaDataSource) {
return new LocalContainerEntityManagerFactoryBean() {{
setDataSource(verticaDataSource);
setPersistenceProviderClass(HibernatePersistenceProvider.class);
setPersistenceUnitName(PERSISTENCE_UNIT_NAME);
setPackagesToScan(PACKAGES_TO_SCAN);
Properties jpaProperties = new Properties();
jpaProperties.put("hibernate.ddl-auto", env.getProperty("vertica.jpa.hibernate.ddl-auto"));
jpaProperties.put("hibernate.show-sql", env.getProperty("vertica.jpa.hibernate.show-sql"));
jpaProperties.put("hibernate.format_sql", env.getProperty("vertica.jpa.hibernate.format_sql"));
jpaProperties.put("hibernate.dialect", env.getProperty("vertica.jpa.properties.hibernate.dialect"));
setJpaProperties(jpaProperties);
afterPropertiesSet();;
}};
}
@Bean
public PlatformTransactionManager verticaTransactionManager(EntityManagerFactory verticaEntityManagerFactory) {
return new JpaTransactionManager(verticaEntityManagerFactory);
}
没有考虑上面 yaml 文件中定义的 Hikari 属性(可能是由于 extends HikariConfig
)。要求是使用默认的 Hikari 属性并覆盖上面定义的属性。请建议如何做到这一点。
不要扩展 HikariConfig
。
使用 HikariConfig()
的默认构造函数获取 Hikari 的默认配置属性,然后根据您的属性文件覆盖您需要的内容,就像您已经为 jpa
属性所做的那样。
@Bean
public HikariDataSource verticaDataSource() {
HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setConnectionTimeout(env.getProperty("vertica.datasource.hikari.connectionTimeout", Long.class));
hikariConfig.setIdleTimeout(env.getProperty("vertica.datasource.hikari.idleTimeout", Long.class));
hikariConfig.setMaxLifetime(env.getProperty("vertica.datasource.hikari.maxLifetime", Long.class));
hikariConfig.setKeepaliveTime(env.getProperty("vertica.datasource.hikari.keepaliveTime", Long.class));
hikariConfig.setMaximumPoolSize(env.getProperty("vertica.datasource.hikari.maximumPoolSize", Integer.class));
hikariConfig.setMinimumIdle(env.getProperty("vertica.datasource.hikari.minimumIdle", Integer.class));
return new HikariDataSource(hikariConfig);
}
我的 Spring 启动应用程序中有两个数据源,因此我无法使用标准数据源属性。其中之一定义如下:
vertica:
datasource:
jdbc-url: jdbc:vertica://${DR_HOST}:${DR_PORT}/${DR_DB_NAME}
username: ${DR_USER}
password: ${DR_PASSWORD}
driver-class-name: com.vertica.jdbc.Driver
hikari:
connectionTimeout: 30000
idleTimeout: 10000
maxLifetime: 10000
keepaliveTime: 5000
maximumPoolSize: 20
minimumIdle: 5
#poolName: vertica-db-pool
#username: ${DB_USER}
#password: ${DB_PASSWORD}
jpa:
hibernate:
ddl-auto: none
format_sql: true
show-sql: true
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
naming-strategy: org.hibernate.cfg.ImprovedNamingStrategy
properties:
hibernate:
dialect: org.hibernate.dialect.VerticaDialect
@Configuration
@ConfigurationProperties("vertica.datasource")
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "verticaEntityManagerFactory",
transactionManagerRef = "verticaTransactionManager",
basePackages = { "com.yyy.vertica" }
)
public class MyDataSourceConfig extends HikariConfig {
public final static String PERSISTENCE_UNIT_NAME = "vertica";
public final static String PACKAGES_TO_SCAN = "com.xxx.entity";
@Autowired
private Environment env;
@Bean
public HikariDataSource verticaDataSource() {
return new HikariDataSource(this);
}
@Bean
public LocalContainerEntityManagerFactoryBean verticaEntityManagerFactory(
final HikariDataSource verticaDataSource) {
return new LocalContainerEntityManagerFactoryBean() {{
setDataSource(verticaDataSource);
setPersistenceProviderClass(HibernatePersistenceProvider.class);
setPersistenceUnitName(PERSISTENCE_UNIT_NAME);
setPackagesToScan(PACKAGES_TO_SCAN);
Properties jpaProperties = new Properties();
jpaProperties.put("hibernate.ddl-auto", env.getProperty("vertica.jpa.hibernate.ddl-auto"));
jpaProperties.put("hibernate.show-sql", env.getProperty("vertica.jpa.hibernate.show-sql"));
jpaProperties.put("hibernate.format_sql", env.getProperty("vertica.jpa.hibernate.format_sql"));
jpaProperties.put("hibernate.dialect", env.getProperty("vertica.jpa.properties.hibernate.dialect"));
setJpaProperties(jpaProperties);
afterPropertiesSet();;
}};
}
@Bean
public PlatformTransactionManager verticaTransactionManager(EntityManagerFactory verticaEntityManagerFactory) {
return new JpaTransactionManager(verticaEntityManagerFactory);
}
没有考虑上面 yaml 文件中定义的 Hikari 属性(可能是由于 extends HikariConfig
)。要求是使用默认的 Hikari 属性并覆盖上面定义的属性。请建议如何做到这一点。
不要扩展 HikariConfig
。
使用 HikariConfig()
的默认构造函数获取 Hikari 的默认配置属性,然后根据您的属性文件覆盖您需要的内容,就像您已经为 jpa
属性所做的那样。
@Bean
public HikariDataSource verticaDataSource() {
HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setConnectionTimeout(env.getProperty("vertica.datasource.hikari.connectionTimeout", Long.class));
hikariConfig.setIdleTimeout(env.getProperty("vertica.datasource.hikari.idleTimeout", Long.class));
hikariConfig.setMaxLifetime(env.getProperty("vertica.datasource.hikari.maxLifetime", Long.class));
hikariConfig.setKeepaliveTime(env.getProperty("vertica.datasource.hikari.keepaliveTime", Long.class));
hikariConfig.setMaximumPoolSize(env.getProperty("vertica.datasource.hikari.maximumPoolSize", Integer.class));
hikariConfig.setMinimumIdle(env.getProperty("vertica.datasource.hikari.minimumIdle", Integer.class));
return new HikariDataSource(hikariConfig);
}