Author: welcome to see my article, doomed to our fate this life! I have been engaged in Java development education for more than 10 years. With the feelings of teaching and educating people, I am making more students avoid detours and changing the fate of millions of IT people!

I hope you can use your hands to like and pay attention to "Brother Pei", and share it with more friends to learn and communicate with each other. You can't keep updating every day without your support! 3Q

Welcome to my B station, you can watch the accompanying video of this article ~~~Please pay attention to my official account for more information ~~~


Learning goals

  • Understands connection pooling
  • Ability to use Druid connection pools
  • Ability to write connection pooling utility classes

Chapter 1 Connection pool

1.1 Overview of Connection Pools

Why use connection pooling

Objective: In order to solve the problem that establishing database connection consumes a lot of resources and time, improve the performance.

The Connection object creates an object when JDBC is used, and the object is destroyed (close) when it is used. Each creation and destruction of an object is a time-consuming operation. It needs to be optimized with connection pooling.

When the program initializes, it initializes multiple connections and puts multiple connections into a pool (collection). Each time you fetch it, you can fetch it directly from the connection pool. When it is finished, the connection is returned to the pool.

The connection pool example in life

  • Old way:

    When I got off the subway, I had to ride my bike, produce one, and then when I was done, I destroyed it.

  • Connection pool mobike:

    Before riding, there is a company produced a lot of bikes, off the subway need to ride, just scan the code to use, and then after riding, return

Connection Pooling Principle

  1. The program begins by creating a number of connections in a container called a connection pool (equivalent to a cupboard/container).
  2. When used, the connection object is directly fetched from the connection pool.
  3. Closing does not actually close the connection, but rather puts the connection object back into the connection pool.

1.2 Writing standard data sources (specifications)

Java provides a common interface for database connection pools: javax.sql.datasource. Vendors need to implement this interface for their own connection pools. This allows the application to easily switch between connection pools from different vendors!

Common third-party (high-performance) connection pools are as follows:

  • C3P0 open source free connection pool! Currently, open source projects using it include: Spring, Hibernate, etc. To use the C3P0 connection pool, you need to import the JAR package. To use the C3P0 connection pool, you need to add the configuration file c3P0-config.xml.
  • Fully functional Druid: Druid is a project on Alibaba’s open source platform that consists of a database connection pool, a plug-in framework, and a SQL parser. Druid is by far the best database connection pool in the country, outperforming other database connection pools (DBCP, C3P0, BoneCP, Proxool, JBoss DataSource) in terms of functionality, performance, and scalability. Druid is a database connection pool developed by Alibaba for monitoring.

  • Performance unmatched HikariCP: HikariCP is a database connection pool component open source by Japanese programmers, the code is very lightweight, and very fast. According to official data, in i7, 32 threads open 32 connections, random database read and write operations, HikariCP is hundreds of times faster than C3P0 database connection pool. HikariCP is also officially recommended in SpringBoot2.0.

1.3 Druid Connection Pool

Druid is a database connection pool developed by Alibaba for monitoring. Druid is the best database connection pool in China. It outperforms other database connection pools in terms of functionality, performance and scalability. Druid has already deployed more than 600 applications on Alibaba, and has been rigorously deployed in a production environment for over a year. Such as: the annual Double 11 activities, the annual Spring Festival rush to grab train tickets.

Druid can be downloaded from github.com/alibaba/dru…

Jar package used by the DRUID connection pool: druid-1.1.16.jar

Druid connection pool utility class

Steps:

  1. Import the DRUID JAR package
  2. Copy configuration files to the Resources directory
  3. Create a connection pool object from the configuration file
  4. Get the connection from the connection pool object

Implementation:

Create druid.properties and place it in the SRC directory

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mydb? useUnicode=true&characterEncoding=UTF-8&useSSL=false&use&serverTimezone=Asia/Shanghai
username=root
password=root
Copy the code

Writing Java code

/** * Alibaba connection pool Druid tools */
public class DruidUtils {
    /* 1. Load the druid.properties configuration file 2. Create a Druid connection pool object. 3. Provides a method */ to get the Connection object Connection from the Connection pool

    public static DataSource ds = null;

    static {
        try {
            //1. Load the druid.properties configuration file
            InputStream is = DruidUtils.class.getClassLoader().getResourceAsStream("druid.properties");
            Properties prop = new Properties();
            prop.load(is);
            //2. Create Druid connection pool objects
           ds = DruidDataSourceFactory.createDataSource(prop);
        } catch(Exception e) { e.printStackTrace(); }}/* 3. Provide a method to get the connection pool object */
    public static DataSource getDataSource(a){
        return ds;
    }

    /* 4. Provide a method to get the Connection object from the Connection pool */
    public static Connection getConnetion(a) throws SQLException {
        Connection conn = ds.getConnection();
        returnconn; }}Copy the code