I'm configuring HikariCP for Spring Boot application, the database is Postgresql.
The documentation says:
"We recommended using dataSourceClassName instead of jdbcUrl, but either is acceptable."
However, the next line says:
"Note: Spring Boot auto-configuration users, you need to use jdbcUrl-based configuration."
If we use jdbcUrl-based configuration and specify dataSourceClassName then jdbcUrl will be ignored, if we don't specify data source - HikariDataSource will be created. So they recommend using HikariDataSource for Spring Boot apps.
If we use dataSourceClassName - it will be created with given properties (in my case it is PGSimpleDataSource with its ancestor BaseDataSource).
Both these configurations work for me.
So, my questions are:
What is the difference between HikariDataSource and PGSimpleDataSource (or any other recommended)?
Why it is recommended to use jdbcUrl-based configuration (and thus HikariDataSource) in Spring Boot?
HikariCP is a connection pool, and a very good one. We've been using it in several projects in production and it's fast and just works.
If you want to use HikariCP you use HikariDataSource. Spring Boot has started to use it as a default and recommends it (for the same reasons: it's fast and solid).
If you just use the default configuration with spring.datasource.url, it will use HikariCP and should work out-of-the-box.
However, when you manually configure your datasource(s), there is a small issue with Spring Boot 2 and HikariCP. HikariCP expects jdbcUrl or dataSourceClassName, but the Spring Boot configuration property uses url.
See the documentation or this question for that.
Related
I have a springboot application where I am trying to add following to application.properties file
spring.datasource.initialize=false
When I add this I see a warning as below:
I tried finding out what's the new property that replaces this deprecated property but in vain.
Can anybody help on this!
Having a reference to a migration guide would be great.
In Spring Boot 2.5, 'spring.datasource.initialization-mode' has been depracated as well:
https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.5-Release-Notes#SQL-Script-DataSource-Initialization
you should use:
spring.sql.init.mode=always
or
spring.sql.init.mode=never
You can read more at:
https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto.data-initialization
As per the document
Spring Boot automatically creates the schema of an embedded
DataSource. This behaviour can be customized by using the
spring.datasource.initialization-mode property. For instance, if you
want to always initialize the DataSource regardless of its type:
spring.datasource.initialization-mode=always
Look at this migration guide
As per Spring Boot Migration mentioned in Github
Basic DataSource initialization is now only enabled for embedded data
sources and will switch off as soon as you’re using a production
database. The new spring.datasource.initialization-mode (replacing
spring.datasource.initialize) offers more control.
spring.datasource.initialization-mode=always
The property spring.datasource.initialization-mode from Spring boot verion 2.7 and onwards is not any more depracated. It has been completely removed!
So the change into the replacement property spring.sql.init.mode is a must do from now on.
Spring Boot 2.7 changelog
You can use spring.jpa.defer-datasource-initialization. Refer to this Spring Documentation on how to Initialize a Database Using Basic SQL Scripts:
spring.jpa.defer-datasource-initialization=true
spring.sql.init.enabled=true - to initialize database by data.sql script located in application resources
spring.sql.init.enabled=false - to
In Spring Boot I can pass properties which will be picked for auto configuration:
spring.ldap.url
spring.ldap.username
spring.ldap.password
Is there any spring.ldap.* property to set pooled=true to avoid using explicit LdapTemplate config?
LdapContextSource contextSource = new LdapContextSource();
contextSource.setPooled(true);
According to the Appendix that lists all of the Spring Boot properties, no. I also looked at the LdapProperties class which stores these values at runtime and didn't see anything in there that would help with pooling. I suspect you'll have to keep doing this manually.
Perhaps file a PR on them? They seem open to adding things if there is a need in the community.
You can set ldap pool properties with JVM parameters. You specify them when launching your application.
For example:
-Dcom.sun.jndi.ldap.connect.pool.maxsize=10
-Dcom.sun.jndi.ldap.connect.pool.prefsize=5
-Dcom.sun.jndi.ldap.connect.pool.timeout=300000
Everything that I have read on this recommends that configuration be done using the PoolingContextSource. The LDAP authentication process requires a two-stage process that is problematic. The below is from the blog of Mattias Hellborg Arthursson, a Spring LDAP guru.
Built-in JNDI Connection Pooling
The pooled property of ContextSource has previously defaulted to true,
enabling the built-in Java LDAP connection pooling by default. However
the built-in LDAP connection pooling suffers from several deficiencies
(most notable there is no way of doing connection validation and
configuration is cumbersome), which is why we decided to change the
default to false . If you require connection pooling we strongly
recommend using the Spring LDAP PoolingContextSource instead.
https://blog.jayway.com/2008/10/27/whats-new-in-spring-ldap-13/
https://docs.spring.io/spring-ldap/docs/1.3.2.RELEASE/reference/html/pooling.html
I have a Spring Boot micro service that connects to several databases through a JDBC connection using JDBCTemplate:
#Bean(name = "mysqlJdbcTemplate")
public JdbcTemplate jdbcTemplate(#Qualifier("mysqlDb") DataSource dsMySQL) {
return new JdbcTemplate(dsMySQL);
}
I have different template for each database and then rest template controller that chooses the right template to use depending on the parameters passed in the request.
I read the documentation, however it's not clear:
Is a connection pool used out of the box or I need to specify it through configuration?
In this case is a connection pool used for each JDBCTemplate?
Spring Boot will try to load available connection pool for your DataSource:
Spring Boot uses the following algorithm for choosing a specific implementation:
We prefer HikariCP for its performance and concurrency. If HikariCP is available, we always choose it.
Otherwise, if the Tomcat pooling DataSource is available, we use it.
If neither HikariCP nor the Tomcat pooling datasource are available and if Commons DBCP2 is available, we use it.
If you use the spring-boot-starter-jdbc or spring-boot-starter-data-jpa “starters”, you automatically get a dependency to HikariCP.
You can bypass that algorithm completely and specify the connection pool to use by setting the spring.datasource.type property. This is especially important if you run your application in a Tomcat container, as tomcat-jdbc is provided by default.
Additional connection pools can always be configured manually. If you define your own DataSource bean, auto-configuration does not occur.
Example of defining your own bean:
#Bean
public DataSource dataSource() throws PropertyVetoException {
return MyDataSourceHolder.getDataSource();
}
I have the following tomcat connection pool configuration from spring-boot-1.x. As of spring 2 the default is hikaricp. How can I migrate the following properties to the hikari pendant? I could not find them:
spring.datasource.tomcat.test-on-borrow=true
spring.datasource.tomcat.initial-size=1
spring.datasource.tomcat.max-idle
spring.datasource.tomcat.initial-size
spring.datasource.tomcat.max-age
spring.datasource.tomcat.test-while-idle
spring.datasource.tomcat.test-on-connect
spring.datasource.tomcat.test-on-return
spring.datasource.tomcat.time-between-eviction-runs-mills
spring.datasource.hikari.*?
You can find all possible properties for HikariCP here.
Although some of the properties you define, are nonexistant in Hikari because of a completely different implementation. Some of them are not even needed anymore.
In my spring-boot application I have an existing dataSource, which I use for Hibernate and/or JdbcTemplate.
I am planning to use spring-session with spring-session-jdbc in the future.
Can the already existing and configured dataSource of the application be used?
If yes, how?
Or do I need to configure an additional dataSource for spring-session-jdbc?
The answer is:
Yes, it is possible, like the documentation of the newly released Spring Session 1.2.0 states:
http://docs.spring.io/spring-session/docs/current/reference/html5/guides/httpsession-jdbc-boot.html#httpsession-jdbc-boot-configuration
It also works without Spring Boot. In an old Spring-MVC project based on xml-config, which does not use Spring Boot, the configured dataSource is automatically used by Spring Session.