Spring-Boot is a pretty awesome tool, but the documentation is a bit sparse when it comes to more advanced configuration. How can I set properties like the maximum size for my database connection pool?
Spring-Boot supports tomcat-jdbc
, HikariCP
and Commons DBCP
natively are they all configured the same way?
maximum-pool-size= 10 #maximum pool size spring. datasource. hikari.
You can still configure it by increasing the max connections (the default is 10 for instance).
Spring Boot uses HikariCP as the default connection pool, due to its remarkable performance and enterprise-ready features.
n is the number of connections allowed per pool, from 1 to 2,147,483,647 (the default). The number of connections is limited by the number of connections supported by your database driver.
It turns out setting these configuration properties is pretty straight forward, but the official documentation is more general so it might be hard to find when searching specifically for connection pool configuration information.
To set the maximum pool size for tomcat-jdbc, set this property in your .properties or .yml file:
spring.datasource.maxActive=5
You can also use the following if you prefer:
spring.datasource.max-active=5
You can set any connection pool property you want this way. Here is a complete list of properties supported by tomcat-jdbc
.
To understand how this works more generally you need to dig into the Spring-Boot code a bit.
Spring-Boot constructs the DataSource like this (see here, line 102):
@ConfigurationProperties(prefix = DataSourceAutoConfiguration.CONFIGURATION_PREFIX) @Bean public DataSource dataSource() { DataSourceBuilder factory = DataSourceBuilder .create(this.properties.getClassLoader()) .driverClassName(this.properties.getDriverClassName()) .url(this.properties.getUrl()) .username(this.properties.getUsername()) .password(this.properties.getPassword()); return factory.build(); }
The DataSourceBuilder is responsible for figuring out which pooling library to use, by checking for each of a series of know classes on the classpath. It then constructs the DataSource and returns it to the dataSource()
function.
At this point, magic kicks in using @ConfigurationProperties
. This annotation tells Spring to look for properties with prefix CONFIGURATION_PREFIX
(which is spring.datasource
). For each property that starts with that prefix, Spring will try to call the setter on the DataSource with that property.
The Tomcat DataSource is an extension of DataSourceProxy, which has the method setMaxActive()
.
And that's how your spring.datasource.maxActive=5
gets applied correctly!
I haven't tried, but if you are using one of the other Spring-Boot supported connection pools (currently HikariCP or Commons DBCP) you should be able to set the properties the same way, but you'll need to look at the project documentation to know what is available.
At the current version of Spring-Boot (1.4.1.RELEASE) , each pooling datasource implementation has its own prefix for properties.
For instance, if you are using tomcat-jdbc:
spring.datasource.tomcat.max-wait=10000
You can find the explanation out here
spring.datasource.max-wait=10000
this have no effect anymore.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With