2022-08-20 数据库连接池

每次去初始化一个连接池,连接池中会有很多个连接等待被使用,使用完连接之后,不需要关闭连接,只需要把连接还回到连接池,还回到连接池的操作不需要我们手动控制。

数据库连接池负责分配、管理和释放数据库连接,它允许应用程序重复使用一个现有的数据库连接,而不是再重新建立一个。

Why?我们为什么要使用它呢?

数据库连接是一种关键的有限的昂贵的资源,一个数据库连接对象均对应一个物理数据库连接,每次操作都打开一个物理连接,使用完都关闭连接,这样造成系统的性能低下。每次去初始化一个连接池,连接池中会有很多个连接等待被使用。使用完连接之后,不需要关闭连接,只需要把连接还回到连接池,还回到连接池的操作不需要我们手动控制。初始化连接池,10条连接,来了20个请求,10个请求就直接拿10条连接去办事。剩下的10个请求,再向服务器申请连接数。 连接池技术尽可能多地重用了消耗内存地资源,大大节省了内存,提高了服务器地服务效率,能够支持更多的客户服务。通过使用连接池,将大大提高程序运行效率。

  • (1)C3P0,2代数据库连接池,太老了
  • (2)DBCP,2代数据库连接池,太老了
  • (3)Druid(德鲁伊)数据库连接池,最好用的连接池。

阿里巴巴开源平台上的一个数据库连接池实现,整合了C3P0和DBCP各自的优点,加入了日志监控,可以监控sql语句的执行情况。

  • (4)Hikari(光),目前最快的连接池。springboot默认的连接池。
    必须有对应的属性文件:.properties。约定 > 配置 > 编码

JDBC使用数据库连接的必要性:在使用基于web程序的数据库连接

1、在主程序中建立连接
2、执行SQL
3、断开连接

点击查看代码,TeacherDao类

package com.jsoft.morning.demo1;

import com.jsoft.util.BaseDao;

import javax.xml.transform.Result;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TeacherDao extends BaseDao {

    public int saveTeacher(Teacher teacher) {

        Connection conn = null;
        PreparedStatement pstmt = null;
        String sql = "insert into teacher (name) values (?)";

        try {
            conn = DATA_SOURCE.getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, teacher.getName());

            return pstmt.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            release(pstmt, null);
        }

    }

    public int updateTeacher(Teacher teacher) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        String sql = "update teacher set name = ? where id = ?";

        try {
            conn = DATA_SOURCE.getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, teacher.getName());
            pstmt.setInt(2, teacher.getId());

            return pstmt.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            release(pstmt, null);
        }
    }

    public int deleteTeacher(Integer id) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        String sql = "delete from teacher where id = ?";

        try {
            conn = DATA_SOURCE.getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, id);

            return pstmt.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            release(pstmt, null);
        }
    }

    public List<teacher> findAllTeacher() {

        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sql = "select id,name from teacher";

        List<teacher> teachers = new ArrayList<>(16);

        try {
            conn = DATA_SOURCE.getConnection();
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();

            while(rs.next()) {
                Teacher teacher = new Teacher(rs.getInt(1),rs.getString(2));
                teachers.add(teacher);
            }
            return teachers;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * &#x67E5;&#x8BE2;&#x67D0;&#x4E00;&#x4E2A;&#x8001;&#x5E08;
     */
    public Teacher findOneTeacher(Integer id) {

        return null;
    }

    /**
     * &#x67E5;&#x8BE2;&#x67D0;&#x4E00;&#x5217;&#x7684;&#x6570;&#x636E;&#xFF1A;&#x7ED3;&#x679C;&#x662F;&#x4E00;&#x884C;&#x4E00;&#x5217;
     */
    public String findTeacherName(Integer id) {

        return null;
    }
}
</teacher></teacher>

点击查看代码 Teacher类

package com.jsoft.morning.demo1;

public class Teacher {

    private Integer id;
    private String name;

    public Teacher() {
    }

    public Teacher(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Teacher{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

点击查看代码 Demo

package com.jsoft.morning.demo1;

import org.junit.Test;

public class Demo {

    @Test
    public void test01() {

        TeacherDao teacherDao = new TeacherDao();
        System.out.println(teacherDao.findAllTeacher());

    }
}

Original: https://www.cnblogs.com/YQuicksilver/p/16614416.html
Author: QuickSilver
Title: 2022-08-20 数据库连接池

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/611103/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球