Apache DBCP Overview and common parameter descriptions, etc.

Source: Internet
Author: User
Tags connection pooling

DBCP (database connection pool), DB connection pool. is a Java Connection pool project on Apache. Because establishing a database connection is a very time-consuming and resource-intensive behavior, the connection pool is pre-established with the database to make some connections, put in memory, the application needs to establish a database connection directly to the connection pool to apply for a line, run out and then put back.

It is also a connection pool component used by open source tools such as Tomcat and Hibernate.

The DBCP version has been upgraded as a whole to the 2.0 era and has been upgraded on the jar package, such as Org.apache.commons.dbcp2.BasicDataSource.

The jar package is: Commons-dbcp2-2.0.1.jar

    • DBCP 2.0.1 for JDBC 4.1 (Java 7+)
    • DBCP 1.4 for JDBC 4 (Java 6)
    • DBCP 1.3 for JDBC 3 (Java 1.4 and Java 5)

Parameters that must be configured

Username the user name to establish the database connection;

Password establish the password of the database connection;

URL passed to the JDBC-driven URL used to establish the connection

Full valid Java class name for the JDBC driver used by driverclassname

ConnectionProperties The connection parameters that are sent to the JDBC driver when a new connection is established, the format must be Key=value format

Connection pooling related configuration

InitialSize Initializing connections: Number of initial connections created when connection pooling starts

Maxactive Maximum Active connection: The maximum number of active connections The connection pool can allocate at the same time, similar to the concurrency, set to 0 for unrestricted, 2.0 versions, the method has been removed, corresponding to the alternative method is:setmaxtotal ();

Maxidle Maximum idle connections: The maximum number of connections that are allowed to remain idle in the connection pool, the idle connections that are exceeded are freed, and are not limited if set to negative numbers.

Minidle Minimum Idle connection: the minimum number of connections allowed to remain idle in the connection pool, below which a new connection will be created and not created if set to 0

maxwait Maximum Connection Wait time: When there is no available connection, the connection pool waits for the maximum time (in milliseconds) for the connection to be returned, an exception is thrown when the time is exceeded, and if set to 1 means Infinity, 2.0 has been removed from the method, the alternative is: Setmaxwaitmillis ();

Other related configurations

Removeabandoned whether to automatically reclaim timeout connections

Removeabandonedtimeout set timeout time has one to note, time-out = current time-the time when the connection is created in the program, if the maxactive is larger, such as more than 100, Then the removeabandonedtimeout can be set a little longer such as 180, that is, three minutes of unresponsive connection for recycling, of course, the different settings of the application length is different.

Timebetweenevictionrunsmillis, Minevictableidletimemillis is used together, every timebetweenevictionrunsmillis milliseconds to check for idle connections in the connection pool, Disconnect a connection that is idle for more than minevictableidletimemillis milliseconds until the number of connections in the connection pool is Minidle

Testonborrow, Testonreturn, Testwhileidle, the meaning of these properties is to obtain, return the object and whether to validate when idle, check whether the object is valid, false by default is not verified. So when using DBCP, when the database connection is broken for some reason, and then gets a connection from the connection pool and does not validate, the connection that was made is actually an invalid database connection. Online a lot of said DBCP bug should be so, only to set these properties to true, and then provide the _validationquery statement to ensure that the database connection is always valid.

An ordinary simple example:

Package test.ffm83.commons.dbcp;

import Org.apache.commons.dbcp.BasicDataSource;

import org.apache.commons.dbcp.BasicDataSourceFactory;

import org.apache.commons.lang.StringUtils;

import java.sql.PreparedStatement;

import Java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Connection;

import java.util.Properties;

/* through DBCP Connection Oracle Database

 * Use 1.4 Version Implementation

* @author Fan Fangming

* */

Public class Dbcpusage {

private static Basicdatasource DataSource =null;

Public Dbcpusage () {

}

Public static void init () {

if (dataSource ! =null) {

Try {

DataSource. Close ();

}catch(Exception e) {

E.printstacktrace ();

}

DataSource =null;

}

Try {

PROPERTIESP = newProperties ();

P.setproperty ("Driverclassname","Oracle.jdbc.driver.OracleDriver");

P.setproperty ("url","jdbc:oracle:thin:@192.168.19.1:1521:fanfangming");

P.setproperty ("password","FFM");

P.setproperty ("username","FFM");

P.setproperty ("maxactive","a");

P.setproperty ("Maxidle","ten");

P.setproperty ("maxwait","n");

P.setproperty ("removeabandoned","false");

P.setproperty ("Removeabandonedtimeout","the");

P.setproperty ("Testonborrow","true");

P.setproperty ("logabandoned","true");

DataSource = (basicdatasource) basicdatasourcefactory

. CreateDataSource (p);

}catch(Exception e) {

E.printstacktrace ();

}

}

Public static synchronized Connectiongetconnection ()throwsSQLException {

if (dataSource = =null) {

Init ();

}

Connectionconn = null;

if (dataSource ! =null) {

conn= dataSource. getconnection ();

}

return Conn;

}

Public static void main (string[] args)throws Exception {

Connectioncon = null;

Try {

Con= Dbcpusage. getconnection ();

Stringsql = "Select Sysdate from dual";

PREPAREDSTATEMENTPS = con.preparestatement (sql);

Resultsetrs = Ps.executequery ();

while (Rs.next ()) {

StringValue = rs.getstring ("sysdate");

System. out. println (StringUtils. Center(value+", database connection successful ", "-"));

}

}catch(Exception e) {

E.printstacktrace ();

}finally{

if (Con! =null) {

Con.close ();

}

}

}

}

Run as follows:

----------2014-12-1612:38:32.0, the database connection was successful-----------

Apache DBCP Overview and common parameter descriptions, etc.

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.