Database connection pool knowledge

Database connection pool

       What is the database connection pool?

       In JDBC, and off each time you create a Connection object will consume a certain amount of time and IO resources. This is because the java program to establish a link between the database, the database side to verify the user name and password, and to allocate resources for this link, the java program should be loaded into memory on behalf of java.sql.Connection object links, etc. Therefore, establishment of a database link is expensive, especially when a large number of concurrent access, if visits a site one day is 100,000, then the site's server you need to create, unlink 100,000 times, frequently create and off open database link is bound to affect the efficiency of access to the database, and may even crash the database.

       To avoid creating a database link frequently, programmers have proposed database connection pool technology. Database connection pool is responsible for the distribution, management and release of database links, which allows an application to reuse existing database link, rather than re-establish

       legend:

      

       Graphic:

              As can be seen from the figure, the database connection pool will create a database link initialization a certain number of connections into the pool when the application program to access the database is not created directly from the Connection, but the connection pool to "apply" a Connection, connection If the connection pool has free, then it is returned, otherwise create a new connection. After use, the connection pool will Connection recovery. And delivered to other threads, in order to reduce the number of on and off to create database links, improve the efficiency of database access.

 

DBCP data source

       DBCP database connection pool (DataBase Connection Pool) short, in the open source Apache connection pool to achieve tissue, Tomcat connection pool is used by the server component, when used alone DBCP data source, need to import two jar package in the application

  1. commons-dbcp.jar
  2. commons-pool.jar

 

Example code:

      

 

       Code:

       public class testDemo {

 

       /*

        * BasicDataSource BasicDataSource class using the class to create a data source object, the manual setting data source object property value, and then obtaining a connection object.

        */

 

       public static DataSource ds = null;

 

       static {

              // Get DBCP data source implementation class object

              BasicDataSource bds = new BasicDataSource();

              // set the configuration information needed to connect to the database

              bds.setDriverClassName("com.mysql.jdbc.Driver");

              bds.setUrl("jdbc:mysql://localhost:3306/demo");

              bds.setUsername("root");

              bds.setPassword("123456");

 

              // set number of links connection pool parameters of the database connection pool initialization

              bds.setınitialsiz to (5);

              // set number of links largest active database connection pool

              bds.setMaxActive(5);

              ds = bds;

 

       }

 

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

 

              // Get the database link object

              Connection conn = ds.getConnection();

              // Get the information database link

              DatabaseMetaData metaData = conn.getMetaData();

              // print information database link

              System.out.println(metaData.getURL());

 

       }

 

}

 

       C3P0 data sources

       C3P0 is one of the most popular open source database connection pool, which implements the DataSource interface to a data source, and support JDBC2 JDBC3 of standards, easy expansion and superior performance, well-known open source framework Hibernate and Spring are used in the data source. When developing the data source using C3P0, C3P0 need to understand the implementation class ComboPooledDataSource DataSource interface, which is the core classes C3P0, there is provided a method of data source objects relevant

       Example code:

       public class testDemo3 {

 

       public static DataSource ds = null;

 

       static {

              ComboPooledDataSource cpds = new ComboPooledDataSource();

              try {

 

                     cpds.setDriverClass("com.mysql.jdbc.Driver");

                     cpds.setJdbcUrl("jdbc:mysql://localhost:3306/demo");

                     cpds.setUser("root");

                     cpds.setPassword("123456");

 

                     cpds.setInitialPoolSize(5);

                     cpds.setMaxPoolSize(5);

 

                     ds = cpds;

 

              } catch (Exception e) {

                     // TODO: handle exception

                     throw new ExceptionInInitializerError(e);

              }

       }

 

       /**

        * @param args

        * @throws SQLException

        */

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

              // TODO Auto-generated method stub

              System.out.println(ds.getConnection());

       }

 

}

Published 40 original articles · won praise 7 · views 10000 +

Guess you like

Origin blog.csdn.net/sj_1993/article/details/105351167