Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring-Boot: How do I set JDBC pool properties like maximum number of connections?

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?

like image 762
JBCP Avatar asked Aug 29 '14 17:08

JBCP


People also ask

What is spring datasource maximum pool size?

maximum-pool-size= 10 #maximum pool size spring. datasource. hikari.

What is the default connection pool size in spring boot?

You can still configure it by increasing the max connections (the default is 10 for instance).

Which connection pool is best for spring boot?

Spring Boot uses HikariCP as the default connection pool, due to its remarkable performance and enterprise-ready features.

What is Max connection pool size?

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.


2 Answers

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!

What about other connection pools

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.

like image 143
JBCP Avatar answered Sep 29 '22 00:09

JBCP


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.

like image 44
Daniel Nuss Avatar answered Sep 29 '22 00:09

Daniel Nuss