JAVA--DBCP Connection Pool

Source: Internet
Author: User
Tags connection pooling

Connection Pool

In real development, "Get Connected" or "release resources" is a very consuming system resources of two processes, in order to solve such performance problems, usually we use the connection pooling technology, to share the connection Connection. So that we don't have to create the connection each time, release the connection, these operations are given to the connection pool

Concept

Use pools to manage Connectionso that you can reuse Connection. With the pool, we don't have to create Connectionourselves, but instead we get the Connection object through the pool. When Connection is finished, calling Connection 's Close () method does not actually close Connection, but instead Connection " return" to the pool. The pool will be able to use this Connection object again.

Specification

Java provides a common interface for database connection pooling:javax.sql.DataSource, each vendor needs to have its own connection pool implement this interface. So the application can easily switch the connection pool of different vendors!

Common connection pools:DBCP,c3p0.

DBCP Connection Pool

DBCP is also an open source connection pool, one of the Apache Common members, also more common in enterprise development,Tomcat 's built-in connection pool.

Common Configuration Items

Tool class

/* *  use DBCP to implement database connection pool *  most basic four complete *  for database connection pool other configurations, custom */import Javax.sql.datasource;import Org.apache.commons.dbcp.basicdatasource;public class jdbcutils{    //Create Basicdatasource class object private static Basicdatasource DataSource = new Basicdatasource ();//static code block, custom configuration for Basicdatasource object static{//database connection information, Required Datasource.setdriverclassname ("Com.mysql.jdbc.Driver");d Atasource.seturl ("jdbc:mysql://localhost:3306/day33 _user ");d atasource.setusername (" root ");d Atasource.setpassword (" 123 ");//Configure the number of connections in the connection pool, Optional datasource.setinitialsize (10);//number of connections initialized datasource.setmaxactive (8);//maximum number of connections Datasource.setmaxidle (5);// Maximum idle number datasource.setminidle (1);//Minimum idle}//defines a static method that returns the object of the Basicdatasource class public static DataSource Getdatasource () { return datasource;}}

Test class

/* * Test Written tool class, * provided is a DataSource interface data source * Queryrunner class construction method, receive DataSource interface implementation class * behind, call method Update,query, without passing them connection connection Object */import java.sql.sqlexception;import Java.util.list;import org.apache.commons.dbutils.queryrunner;import Org.apache.commons.dbutils.handlers.arraylisthandler;import Cn.itcast.jdbcutils.jdbcutils;public Class queryrunnerdemo{public static void Main (string[] args) {select ();} Defines 2 methods, implements the data table to add, the data table queries the//queryrunner class object, writes in the class member position private static queryrunner QR = new Queryrunner ( Jdbcutils.getdatasource ()); Data table query public static void Select () {String sql = ' select * from sort ';try{list<object[]> List = qr.query (sql, new ARR Aylisthandler ()); for (object[] objs:list) {for (Object obj:objs) {System.out.print (obj+ "\ t");} System.out.println ();}} catch (SQLException ex) {throw new RuntimeException ("Data query Failed");}} Data table add data public static void Insert () {String sql = ' insert into sort (SNAME,SPRICE,SDESC) VALUES (?,?,?) "; O Bject[] params = {"Fruit", 100.12, "Just listed walnut"};try{int row = Qr.update(SQL, params); No need to add conn connection System.out.println (row);} catch (SQLException ex) {throw new RuntimeException ("Data addition failed");}}}

JAVA--DBCP 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.