I am using spring-boot in my web application and use spring-jpa to read/write from/to my database. It works very well but I want to understand how to manage the database connections. Below is my properties configuration for database:
spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8 spring.datasource.username=user spring.datasource.password=pwd spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.max-active=500
I have set the maximum connections to 500. When a user makes a request on my spring application, a database connection will be opened for him. After finishing the request, will spring jpa close this connection? If not, when will it close the unused connections?
I have read through the spring jpa reference document from http://docs.spring.io/spring-data/jpa/docs/current/reference/html/. But it doesn't mention anything about the connections.
Here's how Spring Boot automatically configures a connection pool datasource: Spring Boot will look for HikariCP on the classpath and use it by default when present. If HikariCP is not found on the classpath, then Spring Boot will pick up the Tomcat JDBC Connection Pool, if it's available.
The default connection pool in Spring Boot 2 is HikariCP. It provides enterprise-ready features and better performance. HikariCP is a JDBC DataSource implementation that provides a connection pooling mechanism. If the HikariCP is present on the classpath, the Spring Boot automatically configures it.
The application displays the result to the end-user. The application closes the connection, which returns the connection to the pool. Note: The application calls the close() method, which allows the connection to remain open. The pool receives the notification of the close request.
From the JDBC Connection Pool—>Monitoring tab, you can view information about the state of each deployed instance of the selected connection pool. That is, for each server on which the connection pool is deployed, you can see current status information about the connection pool.
When using DB connection pooling, a call to sqlconnection.close()
will not necessarily close the heavyweight connection to the database, instead most often will just release the connection as re-usable in the pool. That's why it is advisable to invoke the close()
on connection as soon as possible when leveraging a client side connection pool.
In your configuration, the pool will contain a maximum number of 500 connections ( it would be also good to configure maxIdle
, minIdle
, and minEvictableIdleTimeMillis
to tune the number of ready-to-use connections and how often to release them when not used).
Some more doc here
You have already found that you can configure this from application.properties
You can find all the possible properties here.
Notice that from Spring Boot 1.4 there are datasource properties for every datasource vendor that spring integrates with, out of the box. There is spring.datasource.dbcp.*
,spring.datasource.tomcat.*
and so on. See 1.4 docs
If that's not enought, and you need something very specific, you can declare the datasource bean yourself. Here is the example with Tomcat datasource:
@Bean public DataSource dataSource(){ PoolProperties p = new PoolProperties(); p.setUrl("jdbc:mysql://localhost:3306/mysql"); p.setDriverClassName("com.mysql.jdbc.Driver"); p.setUsername("root"); p.setPassword("password"); p.setJmxEnabled(true); p.setTestWhileIdle(false); p.setTestOnBorrow(true); p.setValidationQuery("SELECT 1"); p.setTestOnReturn(false); p.setValidationInterval(30000); p.setTimeBetweenEvictionRunsMillis(30000); p.setMaxActive(100); p.setInitialSize(10); p.setMaxWait(10000); p.setRemoveAbandonedTimeout(60); p.setMinEvictableIdleTimeMillis(30000); p.setMinIdle(10); p.setLogAbandoned(true); p.setRemoveAbandoned(true); p.setJdbcInterceptors( "org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;"+ "org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer"); DataSource datasource = new DataSource(); datasource.setPoolProperties(p); return datasource ; }
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