Database Connection Pool

Source: Internet
Author: User
Tags manage connection

Database Connection Pool

What is a database connection pool

A database connection is a critical, limited, and expensive resource. Each time a user gets a database connection consumes a large resource, the database connection pool can manage the connection and can reuse the connection without having to create it every time. Using pools to manage the lifecycle of a connection can save resources and improve performance. Use pools to manage connection, which can be reused with connection. With the pool, we don't have to create connection ourselves, but we get the connection object through the pool. When connection is finished, calling connection's close () method does not actually close the connection, but instead connection "returns" to the pool. The pool will be able to use this connection object again. The principle of database connection pool in layman's words: first create several databases in the pool, and so the system needs to use the direct to connect, and make the mark (connection), used up and then the database connection back to the pool, at the same time mark (idle). When there is a connection requirement, there is no idle database connection at this time, and a new connection is placed in the pool. These parameters can be configured on their own, such as:
-Initial Size: 10
-Minimum number of idle connections: 3
-Increment: Minimum units created at a time (5)
-Maximum idle connections: 12
-Maximum connections: 20
-most Large wait time: 1000 milliseconds
Second, common connection pool
Here's a summary of the two common connection pools: DBCP and c3p0.  The guide package is the first step in using the database connection pool,
1.DBCP: the jar package, Commons-pool.jar, Commons-dbcp.jar
DBCP is an open source free database connection pool provided by Apache.

    public void demo1() throws SQLException {        //导入核心类:BasicDataSource        BasicDataSource ds = new BasicDataSource();        //基本配置        ds.setDriverClassName("com.mysql.jdbc.Driver");        ds.setUrl("jdbc:mysql://localhost:3306/mydb1");        ds.setUsername("root");        ds.setPassword("123");                //最大连接数             ds.setMaxActive(20);         //最大空闲连接数:表示即使没有数据库连接时依然可以保持10个空闲的连接,而不被清除,随时处于待命状态。设 0 为没有限制。          ds.setMaxIdle(10);         //初始化连接数            ds.setInitialSize(10) ;        //最小空闲连接数           ds.setMinIdle(2) ;        //最大等待毫秒数        ds.setMaxWait(1000) ;                Connection con = ds.getConnection();        //只是将连接归还池        con.close() ;}


2.C3P0: Guide Jar Pack, C3p0-0.9.2-pre1.jar, C3p0-oracle-thin-extras-0.9.2-pre1.jar, Mchange-commons-0.2.jar
C3P0 is also open source free connection pool!

public void demo2() throws PropertyVetoException, SQLException {    //导入核心类:ComboPooledDataSource    ComboPooledDataSource ds = new ComboPooledDataSource();    //基本配置    ds.setDriverClass("com.mysql.jdbc.Driver");    ds.setJdbcUrl("jdbc:mysql://localhost:3306/mydb1");    ds.setUser("root");    ds.setPassword("123");            //每次的增量为5    ds.setAcquireIncrement(5) ;    //初始化连接数为20    ds.setInitialPoolSize(20) ;    //最少连接数为2    ds.setMinPoolSize(2) ;    //最大连接数为50    ds.setMaxPoolSize(50) ;        Connection con = ds.getConnection();    //只是将连接归还池    con.close();}

Using the configuration file for connection, there are two requirements: (1) configuration file: C3p0-config.xml; (2) file location: src

<?xml version="1.0" encoding="UTF-8"?><c3p0-config>    <default-config>         <property name="jdbcUrl">jdbc:mysql://localhost:3306/mydb1</property>        <property name="driverClass">com.mysql.jdbc.Driver</property>        <property name="user">root</property>        <property name="password">123</property>        <property name="acquireIncrement">3</property>        <property name="initialPoolSize">10</property>        <property name="minPoolSize">2</property>        <property name="maxPoolSize">10</property>    </default-config></c3p0-config>

Multiple connection information can be configured in the C3P0 configuration file, giving each configuration a name so that configuration information can be easily toggled by configuration name. The configuration for MySQL is configured by default in the above file, and the configuration named Test-config is also the configuration of MySQL. The configuration information can be specified using the parameter constructor: Combopooleddatasource ds = new Combopooleddatasource ("Test-config").

    <named-config name="test-config">         <property name="jdbcUrl">jdbc:mysql://localhost:3306/mydb1</property>        <property name="driverClass">com.mysql.jdbc.Driver</property>        <property name="user">root</property>        <property name="password">123</property>        <property name="acquireIncrement">3</property>        <property name="initialPoolSize">10</property>        <property name="minPoolSize">2</property>        <property name="maxPoolSize">10</property>    </named-config>

At the end of the article: I do not know if you have the C3P0 this connection pool name curiosity, on-line search the full name of the next c3p0, found a small story:
According to rumor: C3p0 Connection pool author is Star Wars fan, C3P0 is one of them, and this name contains connection and pool word letter. So call this name. C3P0 is the robot on the right. On the left is his brother R2D2. (Small story Source: 8565864)

Database Connection Pool

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.