# How to Configure Database Connection Pooling with Druid in the Mall Project

> Configure database connection pooling with Druid in the Mall project using Spring Boot YAML. Achieve high-performance database connectivity and built-in monitoring for your microservices.

- Repository: [macro/mall](https://github.com/macrozheng/mall)
- Tags: how-to-guide
- Published: 2026-02-28

---

**The Mall project configures database connection pooling with Alibaba Druid using Spring Boot YAML properties under the `spring.datasource.druid` namespace, enabling high-performance database connectivity with built-in monitoring across all microservices.**

The `macrozheng/mall` repository demonstrates enterprise-grade database connection management by integrating **Alibaba Druid** as its JDBC connection pool. This approach leverages Spring Boot auto-configuration to eliminate boilerplate code while providing production-ready monitoring capabilities through the Druid console.

## Adding the Druid Dependency

Druid is introduced to the project through the `druid-spring-boot-starter` dependency defined in the root [`pom.xml`](https://github.com/macrozheng/mall/blob/main/pom.xml). The repository pins version `1.2.14` in the dependency management section, ensuring all service modules—including `mall-admin`, `mall-portal`, and `mall-search`—inherit the same consistent version.

```xml
<!-- In pom.xml (root) -->
<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid-spring-boot-starter</artifactId>
      <version>1.2.14</version>
    </dependency>
  </dependencies>
</dependencyManagement>

```

With the starter on the classpath, Spring Boot automatically creates a `DruidDataSource` bean without requiring explicit Java configuration, unless custom filters or listeners are needed.

## Configuring Connection Pool Settings via YAML

Each service module ships with environment-specific YAML configurations ([`application-dev.yml`](https://github.com/macrozheng/mall/blob/main/application-dev.yml) and [`application-prod.yml`](https://github.com/macrozheng/mall/blob/main/application-prod.yml)) that define the connection pool parameters under the `spring.datasource.druid` key.

In [`mall-admin/src/main/resources/application-dev.yml`](https://github.com/macrozheng/mall/blob/main/mall-admin/src/main/resources/application-dev.yml), the development profile specifies:

```yaml
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/mall?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false
    username: root
    password: root
    druid:
      initial-size: 5          # initial connections

      min-idle: 10             # minimum idle connections

      max-active: 20           # maximum active connections

      web-stat-filter:
        exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"
      stat-view-servlet:
        login-username: druid
        login-password: druid

```

The **initial-size** parameter establishes five connections on startup, while **min-idle** maintains ten idle connections and **max-active** caps the total at twenty concurrent connections. These values should be tuned according to your specific workload and database server capacity.

The production configuration in [`mall-admin/src/main/resources/application-prod.yml`](https://github.com/macrozheng/mall/blob/main/mall-admin/src/main/resources/application-prod.yml) mirrors this structure with hardened credentials and host values appropriate for production environments.

## Enabling the Druid Monitoring Dashboard

Druid provides a built-in web interface for real-time connection pool statistics, SQL profiling, and wall filters. This is enabled through the `web-stat-filter` and `stat-view-servlet` properties shown above.

The monitoring console is accessible at:

```

http://<host>:<port>/druid/index.html

```

Use the credentials defined in `stat-view-servlet` (default: `druid` / `druid`) to authenticate.

To ensure the monitoring endpoint remains accessible, the global security configuration explicitly excludes the `/druid/**` path from authentication filters. In [`mall-admin/src/main/resources/application.yml`](https://github.com/macrozheng/mall/blob/main/mall-admin/src/main/resources/application.yml), the security whitelist permits unauthenticated access to the Druid console:

```yaml
spring:
  security:
    ignored:
      - /druid/**

```

## Maintaining Cross-Module Consistency

The Mall project enforces uniform database connection behavior across all microservices by replicating the same Druid configuration pattern. The `mall-portal` module defines identical pool settings in [`mall-portal/src/main/resources/application-dev.yml`](https://github.com/macrozheng/mall/blob/main/mall-portal/src/main/resources/application-dev.yml), while `mall-search` implements the same structure in [`mall-search/src/main/resources/application-dev.yml`](https://github.com/macrozheng/mall/blob/main/mall-search/src/main/resources/application-dev.yml).

This consistency ensures that whether a request hits the admin interface, user portal, or search service, the database connection pooling characteristics remain predictable and manageable.

## Customizing Druid Programmatically

While YAML configuration handles most scenarios, you can programmatically customize the `DruidDataSource` when requiring advanced filters like SQL wall protection or custom stat logging.

Create a configuration class to override the auto-configured bean:

```java
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.filter.stat.StatFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Collections;

@Configuration
public class DruidConfig {

    @Bean
    public DruidDataSource druidDataSource() {
        DruidDataSource ds = new DruidDataSource();
        // Basic properties still load from application.yml
        StatFilter statFilter = new StatFilter();
        statFilter.setMergeSql(true);
        ds.setProxyFilters(Collections.singletonList(statFilter));
        return ds;
    }
}

```

This approach allows you to inject custom filters while retaining the YAML-based connection pool sizing.

## Summary

- **Dependency Setup**: Add `druid-spring-boot-starter` version `1.2.14` to the root [`pom.xml`](https://github.com/macrozheng/mall/blob/main/pom.xml) to enable Druid across all modules.
- **YAML Configuration**: Define pool sizing (`initial-size`, `min-idle`, `max-active`) under `spring.datasource.druid` in each module's [`application-dev.yml`](https://github.com/macrozheng/mall/blob/main/application-dev.yml) or [`application-prod.yml`](https://github.com/macrozheng/mall/blob/main/application-prod.yml).
- **Monitoring Access**: Enable the web console via `stat-view-servlet` properties and whitelist `/druid/**` in security configuration to access real-time metrics.
- **Consistency**: Replicate the same Druid configuration structure across `mall-admin`, `mall-portal`, and `mall-search` for uniform behavior.
- **Programmatic Extension**: Override the `DruidDataSource` bean with custom filters when YAML configuration proves insufficient.

## Frequently Asked Questions

### How do I change the maximum number of database connections in the Mall project?

Modify the `max-active` property under `spring.datasource.druid` in your active profile's YAML file (e.g., [`mall-admin/src/main/resources/application-dev.yml`](https://github.com/macrozheng/mall/blob/main/mall-admin/src/main/resources/application-dev.yml)). The default development configuration sets this value to `20`, but you should increase it for high-traffic environments or decrease it to conserve database server resources.

### Where is the Druid monitoring console URL in the Mall application?

Access the monitoring dashboard at `http://<host>:<port>/druid/index.html` after starting any service module. The default login credentials are `druid` for both username and password, as configured in the `stat-view-servlet` section of the YAML files. Ensure the `/druid/**` path is excluded from Spring Security filters to avoid authentication redirects.

### Why does the Mall project use Druid instead of HikariCP?

According to the `macrozheng/mall` source code, Druid is chosen for its **built-in monitoring capabilities** (the `/druid` web console), **configurable filters** (web-stat, wall, and log filters via YAML), and **high-performance** connection acquisition. While HikariCP offers faster raw performance, Druid provides superior observability and SQL profiling features essential for production debugging without additional tooling.

### Can I use a custom password for the Druid monitoring login?

Yes. Change the `login-username` and `login-password` values under `stat-view-servlet` in your [`application.yml`](https://github.com/macrozheng/mall/blob/main/application.yml) or profile-specific configuration files. For production deployments in [`application-prod.yml`](https://github.com/macrozheng/mall/blob/main/application-prod.yml), you should always replace the default `druid/druid` credentials with secure, environment-specific values before deploying to production servers.