Use of the DBCP connection pool

Source: Internet
Author: User
Tags connection pooling

Optimize our operations through connection pooling (data sources). Requirements:    When using JDBC, it is necessary to get the connection (create) and then release the connection (destroy) and optimize the curd operation through the connection pool. Technical Analysis:    Connection pooling Connection Pooling Overview:    Manage database connections,    role:        Improve project performance .    is to deposit a certain number of connections when connection pooling is initialized , use the time through the method to obtain, not the time to return the connection can .    All connection pools must implement an interface Javax.sql.DataSource interface      Get connection method:    & nbsp   Connection getconnection ()     Return the connection method is the previous method of releasing the resource. Call Connection.close (); Customize a connection pool (understanding ideas)   Common connection pools:     dbcp    c3p0  methods     1. Inheritance     2. Decorator mode (static proxy)     3. Dynamic Agent Decorator Mode: ★     Use steps:        1. The decorator and the decorator implement the same interface or inherit the same class         2. The decorator should have a reference to the decorator         3. Strengthen the methods needed for enhancement         4. Call the original method for methods that do not need to be strengthened    common connection pooling:    DBCP: (understanding)         Apache organization         Use steps:            1. Import jar Packages (Commons-dbcp-1.4.jar and Commons-pool-1.5.6.jar) &nbsP           2. Use api                A. Hard-coded     &NBSP ;              //Create Connection pool                   &NB Sp Basicdatasource ds = new Basicdatasource ();                    //With Information                     Ds.setdriverclassname ("Com.mysql.jdbc.Driver");                     Ds.seturl ("jdbc:mysql:///day07");    &NBSP ;               ds.setusername ("root");              & nbsp     Ds.setpassword ("1234"); 
1 //DBCP Code Demo:2  Public classDbcpdemo {3       Public Static voidMain (string[] args) {4           //get links through the DBCP connection pool5Basicdatasource Bdatasource =NewBasicdatasource ();6           //Setting Parameters7Bdatasource.setdriverclassname ("Com.mysql.jdbc.Driver");8Bdatasource.setusername ("root");9Bdatasource.setpassword ("root");TenBdatasource.seturl ("Jdbc:mysql://localhost:3306/test"); OneConnection conn=NULL; A           Try { -               //get links in a linked pool -conn =bdatasource.getconnection (); the SYSTEM.OUT.PRINTLN (conn); -}Catch(SQLException e) { - e.printstacktrace (); -}finally { +               if(conn!=NULL) { -                    Try { +                         //Although the Close method is called, the connection pool modifies the Close method through decorator mode A                         //the actual function is to release the link back into the connection pool at conn.close (); -}Catch(SQLException e) { - e.printstacktrace (); -                    } -               } -           } in      } - } to //Output Result: +Jdbc:mysql://localhost:3306/test, [email protected], MySQL Connector Java
                 B. Profile               &NBSP ;     Implementation write a properties file                    //store config file   &nbsp ;                 Properties prop = new Properties ();        &NB Sp           Prop.load (New FileInputStream ("Src/dbcp.properties"));        & nbsp          //Setup                    //prop.setpro Perty ("Driverclassname", "Com.mysql.jdbc.Driver");                   & nbsp Create Connection pool                     DataSource ds = new Basicdatasourcefactory (). CreateDataSource (prop); 
1 // DBCP through the configuration file code Demo: 2 // configuration file: 3 driverclassname=com.mysql.jdbc.Driver4 username=root5 password= Root 6 Url=jdbc:mysql://localhost:3306/tes
1 //Demo Code:2 ImportJava.io.FileInputStream;3 Importjava.io.FileNotFoundException;4 Importjava.io.IOException;5 Importjava.sql.Connection;6 Importjava.sql.SQLException;7 Importjava.util.Properties;8  9 ImportJavax.sql.DataSource;Ten   One Importorg.apache.commons.dbcp.BasicDataSourceFactory; A /** - * Use connection pooling with configuration files -  * @authorZxy the  * -  */ -  Public classDemo { -       Public Static voidMain (string[] args) { +           //Create a Properties object -Properties prop =NewProperties (); +Connection conn=NULL; A           Try { at                //load configuration file, incoming input stream -Prop.load (NewFileInputStream ("Src/dbcp.properties")); -                //Get links -DataSource bs =Basicdatasourcefactory.createdatasource (prop); -conn =bs.getconnection (); -                //Test Output in SYSTEM.OUT.PRINTLN (conn); -}Catch(FileNotFoundException e) { to e.printstacktrace (); +}Catch(IOException e) { - e.printstacktrace (); the}Catch(Exception e) { * e.printstacktrace (); $}finally {Panax Notoginseng                //Close Link -               if(conn!=NULL) { the                    Try { + conn.close (); A}Catch(SQLException e) { the e.printstacktrace (); +                    } -               } $           } $      } -}

Use of the 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.