Java--JDBC database connection pool

Posted Jun 16, 20203 min read

Java--JDBC database connection pool

Blog description

The information involved in the article comes from the 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), which stores the database connection container.

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

Benefits

save resources

Efficient user access

Implementation

Standard interface:DataSource javax.sql package

  • Get connection:getConnection()
  • Return the connection:Connection.close(). If the connection object Connection is obtained from the connection pool, then calling the Connection.close() method will not close the connection anymore. But return the connection

Generally we don t implement it, database vendors do it.

C3P0

Database connection pool technology

step:

  1. Import jar packages(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 the configuration file:

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

  4. Get connection:getConnection

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

Druid

Database connection pool implementation technology, provided by Alibaba

steps
  1. Import jar package druid-1.0.9.jar

  2. Define the configuration file:

    • Is in the form of properties
    • Can be called any name, can be placed in any directory
  3. Load the configuration file. Properties

  4. Get the database connection pool object:get DruidDataSourceFactory through the 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 the connection pool object
 DataSource ds = DruidDataSourceFactory.createDataSource(pro);
 //5. Get connected
 Connection conn = ds.getConnection();
Define tool class
  1. Define a class JDBCUtils

  2. Provide static code block to load configuration file and initialize connection pool object

  3. Provide method

    1. Get connection method:Get connection through database connection pool

    2. Free resources

    3. How to get the connection pool

    4. Code

      public class JDBCUtils {
      //1. Define 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 connected

      • /
        public static Connection getConnection() throws SQLException {
        return ds.getConnection();
        }

        /**

      • Free 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 the connection
          } catch(SQLException e) {
              e.printStackTrace();
          }

        }
        }

        /**

      • Get connection pool method

      • /

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

thank

Dark Horse Programmer

And hardworking self