Comprehensive Guide to Configuring Druid DataSource in Spring Boot
This article provides a detailed overview of Druid, a high‑performance Java database connection pool, covering its core concepts, Maven dependencies, extensive configuration options, monitoring filters, SQL slow‑query logging, and methods to customize or remove the built‑in advertisement page.
1. Basic Concepts
Druid is considered the best Java database connection pool, offering powerful monitoring and extensibility, and surpassing other pools such as HikariCP, C3P0, DBCP, and BoneCP.
It provides built‑in filters like stat for statistics, wall for SQL injection protection, and log4j2 for logging SQL statements.
2. Adding Dependencies
<!-- Alibaba Druid starter -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.23</version>
</dependency>
<!-- MySQL 8 driver -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- Log4j2 starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<!-- MyBatis starter with Hikari exclusion -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
<exclusions>
<exclusion>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</exclusion>
</exclusions>
</dependency>3. Configuring Properties
Configure the Druid datasource in application.yml , setting JDBC credentials, pool size, validation query, and enabling filters such as stat,wall,log4j2 . Also configure WebStatFilter for web request monitoring and StatViewServlet for the built‑in monitoring UI.
spring:
datasource:
druid:
initial-size: 5
minIdle: 10
max-active: 20
max-wait: 60000
time-between-eviction-runs-millis: 2000
min-evictable-idle-time-millis: 600000
max-evictable-idle-time-millis: 900000
validationQuery: select 1
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxOpenPreparedStatements: 20
keepAlive: true
aop-patterns: "com.springboot.template.dao.*"
filters: stat,wall,log4j2
web-stat-filter:
enabled: true
url-pattern: "/*"
exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"
session-stat-enable: true
session-stat-max-count: 1000
stat-view-servlet:
enabled: true
url-pattern: /druid/*
reset-enable: false
login-username: root
login-password: 123
allow: 127.0.0.14. Monitoring Pages
After starting the application, access /druid/login.html to view the monitoring dashboard, which includes data source information, SQL execution statistics, URL access stats, Spring AOP monitoring, SQL firewall details, session monitoring, and a JSON API.
5. SQL Monitoring
Enable WebStatFilter to collect SQL execution time, request counts, and related database activity for each web request.
6. Slow SQL Logging
Configure the StatFilter with log-slow-sql: true and set slow-sql-millis (e.g., 2000 ms) to record slow queries in the logs.
7. Spring Monitoring
Add the spring-boot-starter-aop dependency and set spring.datasource.druid.aop-patterns to monitor method execution times via AOP.
8. Removing Druid Advertisement
Two approaches: (1) manually comment out the footer code in common.js inside the Druid JAR; (2) register a custom filter that intercepts the common.js request and strips the advertisement HTML using regex.
9. Accessing Druid Statistics Programmatically
Use DruidStatManagerFacade#getDataSourceStatDataList to retrieve datasource monitoring data, or expose it via a REST controller:
@RestController
@RequestMapping("/druid")
public class DruidStatController {
@GetMapping("/stat")
public Object druidStat() {
return DruidStatManagerFacade.getInstance().getDataSourceStatDataList();
}
}Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.