DBCP connection pool introduction and dbcp Introduction

Source: Internet
Author: User

DBCP connection pool introduction and dbcp Introduction

DBCP connection pool Introduction

1. Database Connection Basics

The basics of the database connection pool include the basic concepts of the database connection pool, the working principle of the database connection pool, and the Java open source connection pool. We will introduce these three aspects one by one:

(1) Basic concepts of Database Connection Pool

Database connection is a key, limited, and expensive resource, which is particularly prominent in multi-user Web applications. Database connection management can significantly affect the scalability and robustness of the entire application, and affect the performance indicators of the program. Therefore, the database connection pool is designed to address this issue.

The database connection pool is responsible for allocating, managing, and releasing database connections. It allows applications to reuse an existing database connection, instead of re-establishing a database connection; release the database connection with idle time exceeding the maximum idle time to avoid database connection omissions due to no released database connection.

During database connection pool initialization, a certain number of database connections are created in the connection pool. The number of connections to these databases is set by the minimum number of database connections. Whether or not these database connections are used, the connection pool will maintain at least the number of connections. The maximum number of database connections in the connection pool limits the maximum number of connections that the connection pool can occupy. When the number of connections requested by the application to the connection pool exceeds the maximum number of connections, these requests will be added to the waiting queue.

(2) working principle of Database Connection Pool

The following describes how a database connection pool works:

 

As shown in the figure, a Web application requests a connection. In fact, a connection is obtained from the connection pool and then returned to the connection pool container after the connection is used.

(3) Java open-source connection pool

Due to Java's open-source features, many Java open-source connection pools have been created. For example, the following database connection pool: Jakarta DBCP, C3P0, Proxool, and DBPool.

Among them, Jakarta DBCP is a database connection pool dependent on the Jakarta commons-pool Object pool. DBCP can be directly used in applications. This is also the technical point to be introduced in this article.

C3P0 is: an open-source JDBC Connection pool, which is released together with Hibernate in the lib directory, including the DataSources object of the Connection pool and Statement pool that implement the jdbc3 and jdbc2 extension specifications.

Here is a brief description of the first two connection pools. Other connection pools are not described here.

 

2. DBCP connection pool Basics

DBCP is the most commonly used Java open-source connection pool. It is generally used in systems with frequent database usage and can process a large number of requests for database connection. It is the preferred database configuration for large sites.

When DBCP is created, it also follows the normal connection pool creation mechanism. For example, when developing with Struts framework, if the DBCP component is configured in the struts-config.xml, the DBCP connection pool will be created at Struts startup.

 

3. Example of DBCP connection pool configuration in Struts Framework

The following is the sample code for configuring the DBCP component in the struts-config.xml file when using the Struts framework:

 

<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE struts-config PUBLIC "-// Apache Software Foundation // DTD Struts Configuration 1.2 // EN"
Http://struts.apache.org/dtds/struts-config_1_2.dtd> <struts-config> <! -- Configure the data source --> <data-sources> <data-source type = "org. apache. commons. dbcp. basicDataSource "> <set-property =" driverClassName "value =" com. mysql. jdbc. driver "/> <set-property =" url "value =" jdbc: mysql: // localhost: 3306/demo "/> <set-property =" username "value =" root "/> <set-property =" password "value =" "/> <set- property = "maxActive" value = "10"/> <set-property = "maxWait" value = "5000"/> <set-property = "defaultAutoCommit" value = "false"/> <set-property = "defaultReadOnly" value = "false"/> <set-property = "validationQuery" value = "select count (*) FROM user "/> </data-source> </data-sources> </struts-config>

 

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.