低脂可可粉 2022-09-13 18:14 采纳率: 0%
浏览 14

关于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
  • 写回答

1条回答 默认 最新

  • 名字被猫咬么了 2022-09-14 17:42
    关注
    评论

报告相同问题?

问题事件

  • 创建了问题 9月13日

悬赏问题

  • ¥15 onlyoffice编辑完后立即下载,下载的不是最新编辑的文档
  • ¥15 求caverdock使用教程
  • ¥15 Coze智能助手搭建过程中的问题请教
  • ¥15 12864只亮屏 不显示汉字
  • ¥20 三极管1000倍放大电路
  • ¥15 vscode报错如何解决
  • ¥15 前端vue CryptoJS Aes CBC加密后端java解密
  • ¥15 python随机森林对两个excel表格读取,shap报错
  • ¥15 基于STM32心率血氧监测(OLED显示)相关代码运行成功后烧录成功OLED显示屏不显示的原因是什么
  • ¥100 X轴为分离变量(因子变量),如何控制X轴每个分类变量的长度。