Use Druid connection pooling in Spring Boot
Introduction to the
Druid connection pool is Alibaba’s open source database connection pool project. Druid connection pooling is designed for monitoring and has built-in powerful monitoring capabilities. The monitoring feature does not affect performance. Powerful, SQL injection resistant, built-in Loging diagnoses Hack application behavior.
Making project address: https://github.com/alibaba/druid
use
There are two common ways to use Druid in Spring Boot. One is to add a Druid dependency and then configure the relevant parameters. The other is to use Druid Spring Boot Starter, which is relatively simple. This paper takes this method as an example.
1. Add dependencies
Add Druid dependency packages to the pom.xml file:
< the dependency > < groupId > com. Alibaba < / groupId > < artifactId > druid - spring - the boot - starter < / artifactId > < version > 1.1.17 < / version > </dependency>
2. Add configuration
The DruidDataSource configuration property list can be viewed with the following configuration property explanation.
- JDBC configuration
Spring. The datasource. The druid. Url = JDBC: mysql: / / 127.0.0.1:3306 / demo? useUnicode=true&characterEncoding=utf8 spring.datasource.druid.username=xxx spring.datasource.druid.password=xxx spring.datasource.druid.driver-class-name=com.mysql.cj.jdbc.Driver
- Connection pool configuration
Spring. The datasource. The druid. Initial - # size = maximum number of connection pool, . The default is 8 spring datasource. The druid. Max - active = spring. The datasource. The druid. Min - idle = spring. The datasource. The druid. Max - wait = spring.datasource.druid.pool-prepared-statements= spring.datasource.druid.max-pool-prepared-statement-per-connection-size= Spring. The datasource. The druid. Max - open - prepared - statements = # verification link is available, Use SQL statements that spring. The datasource. The druid. Validation - query = spring. The datasource. The druid. Validation - query - timeout = spring.datasource.druid.test-on-borrow= spring.datasource.druid.test-on-return= # testWhileIdle = "true" Indicates whether the connection is checked by the idle connection collector (if any). If the test failed, the connection will be removed from the pool. Spring. The datasource. The druid. Test - while - idle = spring. The datasource. The druid. Time - between - eviction - runs - millis = spring.datasource.druid.min-evictable-idle-time-millis= spring.datasource.druid.max-evictable-idle-time-millis= Spring. The datasource. The druid. Filters = # configure multiple commas in English
- Monitoring configuration
# login user name spring. The datasource. The druid. Stat - view - servlet. Login - username = # XXX login password spring.datasource.druid.stat-view-servlet.login-password=xxx
3. Run tests
Add configuration is finished, start the application, use the browser visit: http://127.0.0.1:8080/druid/l.
Enter the login page has been configured username spring. The datasource. The druid. Stat – view – servlet. Login – the username and password Spring. The datasource. The druid. Stat – view – servlet. The login password, after the success of the login to the druid monitoring page:
At this point, Spring Boot integration Druid configuration is complete, more function use and configuration please see the official documentation.
Reference documentation
- Druid Spring Boot Starter