关于Druid连接数的疑惑

问题

为什么Druid数据库连接数,查询的时候需要1个连接数,而插入的时候需要2个连接数?我不主动关闭连接数,一个插入占用的连接数为什么会 -1?

截图

刚开始的连接数

img

查询一次不主动释放的连接数

img

插入一次的连接数 = 3

img

我没有主动释放,但是过了一会就变成了 2

img

代码

Druid配置类

package com.example.druid_test.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.sql.Connection;
import java.sql.SQLException;
import java.util.Objects;

@Configuration
public class DruidConfig {
    private static DataSource dataSource;

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

    /**
     * 获取连接
     */
    public static Connection getConnection() {
        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 归还连接
     */
    public static void close(Connection connection) throws SQLException {
        if (!Objects.isNull(connection)) {
            connection.close();
        }
    }
}

Controller

package com.example.druid_test.controller;

import com.alibaba.druid.pool.DruidDataSource;
import com.example.druid_test.service.DruidService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.sql.DataSource;

@RestController
public class DruidController {
    @Autowired
    private DruidService druidService;

    @Autowired
    private DataSource druidDataSource;

    @PostMapping("/test/select")
    public void selectUserInfo() throws Exception {
        druidService.selectUserInfo();
    }

    @PostMapping("/test/insert")
    public void insertUserInfo() throws Exception {
        druidService.insertUserInfo();
    }

    @GetMapping("/test/connectCount")
    public void connectCount() throws Exception {
        int activeCount = ((DruidDataSource) druidDataSource).getActiveCount();
        System.out.println("当前连接数为:" + activeCount);
    }
}

Service

package com.example.druid_test.service;

import com.alibaba.druid.pool.DruidDataSource;
import com.example.druid_test.config.DruidConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@Service
@Transactional
public class DruidService {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private DataSource druidDataSource;

    /**
     * 查询用户信息
     */
    public void selectUserInfo() throws SQLException {
        Connection connection = DruidConfig.getConnection();
        String selectList = "SELECT * FROM info";
        PreparedStatement preparedStatement = connection.prepareStatement(selectList);
        ResultSet resultSet = preparedStatement.executeQuery();
        if (resultSet.next()) {
            int id = resultSet.getInt(1);
            String username = resultSet.getString(2);
            String age = resultSet.getString(3);
            String sex = resultSet.getString(4);
            String address = resultSet.getString(5);
            String email = resultSet.getString(6);
            logger.info("info = { " + id + ", " + username + ", " + age + ", " + sex + ", " + address + ", " + email + " }");
        }
    }

    /**
     * 插入用户信息
     */
    public void insertUserInfo() throws SQLException, InterruptedException {
        Connection connection = DruidConfig.getConnection();
        String insertSql = "INSERT INTO info(username, age, sex, address, email) VALUES ('LiSi', 22, '女', '北京市朝阳门', '123456789@qq.com') ";
        PreparedStatement preparedStatement = connection.prepareStatement(insertSql);
        int result = preparedStatement.executeUpdate(insertSql);
        logger.info("插入结果:" + (result > 0 ? "success" : "failed") + ", " + ((DruidDataSource) druidDataSource).getActiveCount());
    }
}

接口文件

GET http://localhost:8080/test/connectCount
Accept: application/json

###
POST http://localhost:8080/test/select
Accept: application/json

###
POST http://localhost:8080/test/insert
Accept: application/json

可参考:
http://t.zoukankan.com/curedfisher-p-11803036.html