Databases 7 min read

Using Druid DataSource in Spring Boot: Configuration, Monitoring, and Troubleshooting

This article explains what Druid is, how to add the Druid dependency, configure it in Spring Boot's application.yml, set up monitoring with a custom DruidConfig class, and resolve common errors such as property binding failures and login issues.

Wukong Talks Architecture
Wukong Talks Architecture
Wukong Talks Architecture
Using Druid DataSource in Spring Boot: Configuration, Monitoring, and Troubleshooting

Druid is a high‑performance database connection pool that emphasizes built‑in monitoring capabilities.

The article clarifies the relationship between JDBC (the Java‑to‑DB bridge), data sources, connection pools, and their management.

To use Druid, locate it on Maven Central (e.g., https://mvnrepository.com/search?q=druid) and add the following Maven dependency:

<!-- Druid -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.21</version>
</dependency>

In application.yml set the data source type to com.alibaba.druid.pool.DruidDataSource and configure properties such as initialSize , maxActive , validationQuery , and monitoring filters:

# druid configuration
 dbType: mysql   # database type
 initialSize: 5  # initial connections
 minIdle: 5      # minimum idle connections
 maxActive: 20   # maximum connections
 maxWait: 60000 # max wait time (ms)
 timeBetweenEvictionRunsMillis: 60000
 minEvictableIdleTimeMillis: 300000
 validationQuery: select 'x'
 testWhileIdle: true
 testOnBorrow: false
 testOnReturn: false
 poolPreparedStatements: false
 filters: stat,wall,log4j
 maxPoolPreparedStatementPerConnectionSize: 20
 useGlobalDataSourceStat: true
 connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

Create a DruidConfig.java class to expose the data source as a Spring bean and to register the StatViewServlet and WebStatFilter for monitoring:

package com.jackson0714.springboot.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class DruidConfig {

    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource druid() {
        return new DruidDataSource();
    }

    // Configure StatViewServlet
    @Bean
    public ServletRegistrationBean statViewServlet() {
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
        Map
servletInitParams = new HashMap<>();
        servletInitParams.put("loginUserName", "Admin");
        servletInitParams.put("loginPassword", "abc123");
        servletInitParams.put("deny", "192.168.10.160"); // deny this IP
        servletInitParams.put("allow", ""); // allow all by default
        servletRegistrationBean.setInitParameters(servletInitParams);
        return servletRegistrationBean;
    }

    // Configure WebStatFilter
    @Bean
    public FilterRegistrationBean webStatFilter() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new WebStatFilter());
        Map
filterInitParams = new HashMap<>();
        filterInitParams.put("exclusions", "*.js,*.css,/druid/*"); // exclude static resources
        filterRegistrationBean.setInitParameters(filterInitParams);
        filterRegistrationBean.setUrlPatterns(Arrays.asList("/*")); // filter all requests
        return filterRegistrationBean;
    }
}

After starting the application, the configured properties are reflected in the DataSource, and the Druid monitoring console can be accessed at http:// :8082/druid using the credentials defined above.

Common issues include a binding error for spring.datasource.filters , which can be resolved by adding the Log4j dependency to pom.xml :

<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

Another problem described is that setting loginUsername and loginPassword does not display the login page; the article notes that no solution was found for this issue.

The article concludes with promotional messages encouraging readers to follow the public account and request additional architecture or PMP materials.

JavamonitoringconfigurationSpring BootDruidDatabase Connection Pool
Wukong Talks Architecture
Written by

Wukong Talks Architecture

Explaining distributed systems and architecture through stories. Author of the "JVM Performance Tuning in Practice" column, open-source author of "Spring Cloud in Practice PassJava", and independently developed a PMP practice quiz mini-program.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.