I'm using Hikari 2.6.1 with Spring boot 1.4.2 for a multitenant app, with every tenant (with every new database connection) new instance of hikari gets created, the first instance which is created at startup is configured correctly with the conf provided in application.properties, but the next instances gets the default conf of hikari.
this is my hikari config in application.properties:
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.connection-timeout=300000
spring.datasource.max-lifetime=500000
spring.datasource.idle-timeout=400000
spring.datasource.maximum-pool-size=20
spring.datasource.minimumIdle=20
How could i configure all hikari instances with the config above.
I would suggest to create a Configuration class for DatabaseConfiguration. You may need to configure Hikari as follows
HikariConfig config = new HikariConfig();
config.setMaximumPoolSize(10);
config.setDataSourceClassName(dataSourceClassName);
config.addDataSourceProperty("url", DbUrl);
config.addDataSourceProperty("user", user);
config.addDataSourceProperty("password", password);
HikariDataSource ds = new HikariDataSource(config);
Related
I surprisingly discovered that liquibase creates his own connection pool with default values and therefore holds 10 connections to db. It doesn't use connection pool configured from application.properties. So, I have a couple of questions:
What is necessity of own pool?
How can I configure this pool?
The linked question has indeed a correct answer. But obviously, 10 connections are taken from the default settings of Hikari Pool (it is the default db connection pool starting from Spring Boot 2.0).
So, here is the modified version of the same configuration but with Hikari instead of tomcat-jdbc:
#LiquibaseDataSource
#Bean
public DataSource liquibaseDataSource() {
DataSource ds = DataSourceBuilder.create()
.username(liquibaseDataSourceProperties.getUser())
.password(liquibaseDataSourceProperties.getPassword())
.url(liquibaseDataSourceProperties.getUrl())
.driverClassName(liquibaseDataSourceProperties.getDriver())
.build();
if (ds instanceof HikariDataSource) {
((HikariDataSource) ds).setMaximumPoolSize(2); //10 by default
}
return ds;
}
We are using a SimpleUrlHandlerMapping in our Spring Boot 2.1 application to load mapping information from database:
#Bean
public SimpleUrlHandlerMapping simpleUrlHandlerMapping() {
SimpleUrlHandlerMapping simpleUrlHandlerMapping = new SimpleUrlHandlerMapping();
simpleUrlHandlerMapping.setOrder(Ordered.HIGHEST_PRECEDENCE);
simpleUrlHandlerMapping.setInterceptors(requestMonitoringInterceptor);
Map<String, Object> urlMap = getUrlMapFromDb();
simpleUrlHandlerMapping.setUrlMap(urlMap);
return simpleUrlHandlerMapping;
}
It works fine, but if the mapping is changed then we need to restart the server to load the new mapping during startup. The application administrator doesn't have server access so he/she is not able to restart the application.
Is there any way to reload the mapping from the application itself without restarting the server?
Reload just the war through tomcat manager gui
I have this property file which I store in a GitHub repo:
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.jdbcUrl=*DB URL*
spring.datasource.username=*USERNAME*
spring.datasource.password=*ENCRYPTED PASSWORD*
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.hikari.connection-timeout=30000
spring.datasource.hikari.maximum-pool-size=10
My config server pulls up the details from this repo.
Now I have a client that requires these properties to configure the HikariDataSource.
but when I start the client I get the following Error:
***************************
APPLICATION FAILED TO START
***************************
Description:
Cannot determine embedded database url for database type NONE
Action:
If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active).
Seems to me like Hikari is unable to get these properties at start-up.
I was configuring my Hikari DataSource like so in my config class:
#Bean
#ConfigurationProperties("spring.datasource")
public HikariDataSource dataSource() {
return (HikariDataSource) DataSourceBuilder.create().type(HikariDataSource.class).build();
}
After removing this bean I still get the same error.
Any idea about what I should be doing here?
Thanks.
I was able to fix this by adding #EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class}) to the main class.
The question is " Not able to set spring.datasource.type " ,
In spring boot 1.3 can work , but in spring boot 1.4 can't work , I don't know why ?
The following is my application.properties:
spring.datasource.type= com.alibaba.druid.pool.DruidDataSource
spring.datasource.url = jdbc:mysql://localhost:3306/api-2016
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10
Can anyone tell me why ?
As of Spring Boot 1.4, we no longer map the DataSource instance to spring.datasource so all the customizations (max-active, etc) aren't applied.
Please read the release notes. The customizations on DruidDataSource were never supported and this was working as a side effect. You can restore that behaviour by creating your own datasource bean:
#Bean
#ConfigurationProprties("app.datasource.druid")
public DataSource dataSource() { ... }
And change your druid-specific settings to app.datasource.druid. If you enable the annotation processor you'll get content assistance in your IDE for those keys!
Having said that and poking at the code, I realize now that we effectively broke spring.datasource.type so I've created #6695 to track that issue.
I'm aware that H2 has a boolean property/setting called DATABASE_TO_UPPER, which you can set at least in the connection URL, as in: ;DATABASE_TO_UPPER=false
I’d like to set this to false, but in my Spring Boot app, I don’t explicitly have a H2 connection URL anywhere. Implicitly there sure is a connection URL though, as I can see in the logs:
o.s.j.d.e.EmbeddedDatabaseFactory: Shutting down embedded database:
url='jdbc:h2:mem:2fb4805b-f927-49b3-a786-2a2cac440f44;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false'
So the question is, what's the easiest way to tell H2 to disable DATABASE_TO_UPPER in this scenario? Can I do it in code when creating the H2 datasource with EmbeddedDatabaseBuilder (see below)? Or in application properties maybe?
This is how the H2 database is explicitly initialised in code:
#Configuration
#EnableTransactionManagement
public class DataSourceConfig {
#Bean
public DataSource devDataSource() {
return new EmbeddedDatabaseBuilder()
.generateUniqueName(true)
.setType(EmbeddedDatabaseType.H2)
.setScriptEncoding("UTF-8")
.ignoreFailedDrops(true)
.addScripts("db/init.sql", "db/schema.sql", "db/test_data.sql")
.build();
}
}
Also, I'm telling JPA/Hibernate not to auto-generate embedded database (without this there was an issue that two in-memory databases were launched):
spring.jpa.generate-ddl=false
spring.jpa.hibernate.ddl-auto=none
You can't w\ the generateUniqueName, but if you call setName("testdb;DATABASE_TO_UPPER=false") you can add parameters. I doubt this is officially supported, but it worked for me.
The spring code that generates the connection url is like this:
String.format("jdbc:h2:mem:%s;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false", databaseName)
You may want abandon using explicit creation via EmbeddedDatabaseBuilder. Spring Boot creates H2 instance automatically based on configuration. So I would try this in application.properties:
spring.datasource.url=jdbc:h2:file:~/testdb;DATABASE_TO_UPPER=false