Learning the basics of database connection pool

The author is also the database connection pool for beginners, the main purpose of writing this article is to facilitate their understanding of the relevant content database connection pool, some not so professional and comprehensive description, please bear with me.

First, the database connection pool Overview

Database connection pool is responsible for the distribution, management and release of database connections, which allows an application to reuse an existing database connection, rather than re-create a; release the database connection idle for more than the maximum idle time to avoid because there is no release of the database connection database connection due to omissions. This technology can significantly improve the performance of database operations.

Is a database connection poolcontainer, Which kept many database connection. When a user accesses the database, the database connection pool begins inside to find the appropriate database connection when the user is completed, return the database connection.
Use the database connection pool to save resources and user access is more efficient, because there is no repetition to create a database connection.

Second, the use of the database connection pool

The current mainstream database connection pool has C3P0, DBCP, Tomcat Jdbc Pool, BoneCP, Druid , etc., with regard to the comparison between them can refer to the blog chiefs of mainstream Java database connection pool configuration compared with the actual development . Which may be specifically selected, and DBCP Druid recommendation according to actual situation.

Druid of use

Druid is a database connection pool to achieve Alibaba open-source platform, which combines the advantages of C3P0, DBCP, PROXOOL such as DB pool, while adding log monitoring, can be a good monitor implementation DB connection pool and SQL can be said for surveillance born DB connection pool.
Use Druid, must first download and import corresponding jar package, Druid download . After introduction to the corresponding jar package configuration, the configuration process may refer to the big brother blog Druid (Druid) Detailed profile data pool, and simple implementation , the approach proposed profile. Then you are loading a configuration file (configuration file can be placed in any directory of the project).
After completion of the above preparations, you canDruidDataSourceFactoryDatabase connection pool object, and then throughgetConnectionGet a database connection. The specific implementation, see the code below:

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.util.Properties;

public class DruidTest {

    public static void main(String[] args) throws Exception {

        //加载配置文件
        Properties properties = new Properties();
        InputStream inputStream =
                DruidTest.class.getClassLoader().getResourceAsStream("druid.properties");
        properties.load(inputStream);

        //获取连接池对象
        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);

        //获取连接
        Connection connection = dataSource.getConnection();

        //输出,测试一下
        System.out.println(connection);
    }
}

May be configured in a druid Tools simplify operations, related to avoid writing code for each time in a different class druid inside. druid Tools code as follows:

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * druid连接池工具类
 */
public class JDBCUtils {

    private static DataSource dataSource;

    static {
        //加载配置文件
        Properties properties = new Properties();
        InputStream inputStream =
                JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties");
        try {
            properties.load(inputStream);

            dataSource = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    //获取连接
    public static Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }

    //释放资源
    public static void close(Statement statement, Connection connection) throws SQLException {

        close(null, statement, connection);

    }

    //释放资源
    public static void close(ResultSet resultSet, Statement statement, Connection connection)
            throws SQLException {
        if (resultSet != null) {
            resultSet.close();
        }

        if (statement != null) {
            statement.close();
        }

        if (connection != null) {
            //注意这里是归还数据库连接,而不是关闭
            connection.close();
        }
    }


    //获取连接池
    public static DataSource getDataSource() {
        return dataSource;
    }


}

On subsequent use druid, simply call the relevant methods to change tools, it is convenient.

Here Insert Picture Description
2019.12.22

Published 52 original articles · won praise 59 · views 6826

Guess you like

Origin blog.csdn.net/ataraxy_/article/details/103651457