Java--JDBC database connection pool

Keywords: Programming Database Druid Java JDBC

Java--JDBC database connection pool

Blog description

The information involved in this article comes from Internet collation and personal summary, which means personal learning and experience summary. If there is any infringement, please contact me to delete, thank you!

concept

In fact, it is a container (Collection) for database connection.

After the system is initialized, the container is created and some connection objects are applied in the container. When the user comes to access the database, the connection objects are obtained from the container. After the user accesses the database, the connection objects are returned to the container.

benefit

save resources

Efficient user access

realization

Standard interface: datasource javax.sql Under the bag

  • Get connection: getConnection()
  • Return connection: Connection.close(). If the connection object connection is obtained from the connection pool, the Connection.close() method, the connection will no longer be closed. It's a return connection

Generally, we don't implement it. We have database manufacturers to implement it

C3P0

Database connection pool technology

Step:

  1. Import the jar package (two) c3p0-0.9.5.2.jar mchange-commons-java-0.2.12.jar,

    • Don't forget to import the database driver jar package
  2. Define profile:

    • Name: c3p0.properties or c3p0-config.xml
    • Path: directly put the file in the src directory.
  3. Create core object database connection pool object ComboPooledDataSource

  4. Get connection: getConnection

code
//1. Create database connection pool object
 DataSource ds  = new ComboPooledDataSource();
 //2. Get connection object
 Connection conn = ds.getConnection();

Druid

Database connection pool implementation technology, provided by Alibaba

step
  1. Import the jar package druid-1.0.9.jar
  2. Define profile:
    • It's in the form of properties
    • It can be called any name and placed in any directory
  3. Load the configuration file. Properties
  4. Get database connection pool object: get DruidDataSourceFactory through factory
  5. Get connection: getConnection

code

 //3. Load configuration file
 Properties pro = new Properties();
 InputStream is =   DruidDemo.class.getClassLoader().getResourceAsStream("druid.properties");
 pro.load(is);
 //4. Get connection pool object
 DataSource ds = DruidDataSourceFactory.createDataSource(pro);
 //5. Get connection
 Connection conn = ds.getConnection();
Define tool class
  1. Define a class of JDBC utils
  2. Provide static code block to load configuration file and initialize connection pool object
  3. Provide methods
    1. Get connection method: get connection through database connection pool
    2. Release resources
    3. How to get the connection pool
    4. code
public class JDBCUtils {
  //1. Define the member variable DataSource
    private static DataSource ds;

    static {
        try {
            //1. Load configuration file
            Properties pro = new Properties();
            pro.load(JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
            //2. Get DataSource
            ds = DruidDataSourceFactory.createDataSource(pro);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Get connection
     */
    public static Connection getConnection() throws SQLException {
        return ds.getConnection();
    }

    /**
     * Release resources
     */
    public static void close(Statement stmt, Connection conn) {
        close(null, stmt, conn);
    }

    public static void close(ResultSet rs, Statement stmt, Connection conn) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (conn != null) {
            try {
                conn.close();//Return connection
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * Get connection pool method
     */

    public static DataSource getDataSource() {
        return ds;
    }
}		    

thank

Black horse programmer

And the industrious self

Posted by luke_barnes on Mon, 15 Jun 2020 22:21:15 -0700