Use of C3P0 database connection pool

Source: Internet
Author: User
Tags connection pooling

    • First of all, what is C3P0? The following is the explanation of Baidu Encyclopedia:

C3P0 is an open source JDBC connection pool that implements the data source and Jndi bindings, and supports the standard extensions of the JDBC3 specification and JDBC2. The open source project that currently uses it has hibernate,spring

    • What are the advantages of using a connection pool compared to the JDBC code we write in the usual?

      • Resource Reuse :

Due to the reuse of database connections, there is a high performance overhead associated with frequent creation and release of connections. On the basis of reducing the system consumption, on the other hand, it also increases the stability of the system operating environment.

      • Faster system response Speed:

Database connection pooling during initialization, several database connections are often created to be placed in the connection pool for backup. The initialization of the connection is now complete. For business request processing, direct utilization of existing available connections avoids the time overhead of database connection initialization and release, thus reducing system response time.

      • New means of resource allocation:

For multiple applications sharing the same database system, the application layer can be configured through the database connection pool, the application of a maximum number of available database connections limit, to avoid an application exclusive all database resources.

      • Unified connection management to avoid database connection leaks:

In a more complete database connection pool implementation, it is possible to forcibly reclaim the occupied connection based on the pre-occupancy timeout setting, thus avoiding the potential resource leaks in regular database connection operations.

How do I use C3P0 in my project?

  1. Guide Jar Package:
  2. two ways to establish a C3P0 connection, the first way is the code mode, the demo is as follows:
     Packagecom.wang.utils;Importjava.beans.PropertyVetoException;Importjava.sql.Connection;ImportJava.sql.DriverManager;Importjava.sql.PreparedStatement;ImportJava.sql.ResultSet;Importjava.sql.SQLException;ImportCom.mchange.v2.c3p0.ComboPooledDataSource; Public classJdbcutils {Private StaticConnection Conn; Private StaticCombopooleddatasource ds =NewCombopooleddatasource ();  Public StaticConnection getconnection () {Try{Ds.setdriverclass ("Com.mysql.jdbc.Driver"); Ds.setjdbcurl ("jdbc:mysql://localhost:3306/test?useunicode=true&characterencoding=utf8&useserverprepstmts=true& Prepstmtcachesqllimit=256&cacheprepstmts=true&prepstmtcachesize=256&rewritebatchedstatements=true "); Ds.setuser ("Root"); Ds.setpassword ("123"); Conn=ds.getconnection (); } Catch(SQLException e) {e.printstacktrace (); } Catch(propertyvetoexception e) {E.printstacktrace (); }        returnConn; }

    The second is to use the way to read the configuration file, the requirement is that the configuration file must be named C3p0-config.xml, and placed in the SRC directory , the configuration file is as follows:

    <?XML version= "1.0" encoding= "UTF-8"?><C3p0-config>    <Default-config>         < Propertyname= "Jdbcurl">            <! [Cdata[JDBC:MYSQL://LOCALHOST:3306/TEST?USEUNICODE=TRUE&CHARACTERENCODING=UTF8&USESERVERPREPSTMTS=TR ue&prepstmtcachesqllimit=256&cacheprepstmts=true&prepstmtcachesize=256& Rewritebatchedstatements=true]]>        </ Property>        < Propertyname= "Driverclass">Com.mysql.jdbc.Driver</ Property>        < Propertyname= "User">Root</ Property>        < Propertyname= "Password">123</ Property>          <!--when the connection in the connection pool runs out, c3p0 the number of connections that are fetched at one time. Default:3 -        < Propertyname= "Acquireincrement">3</ Property>         <!--number of connections when initializing a database connection pool -        < Propertyname= "Initialpoolsize">10</ Property>        <!--minimum number of database connections in the database connection pool -        < Propertyname= "Minpoolsize">2</ Property>        <!--Maximum number of database connections in a database connection pool -        < Propertyname= "Maxpoolsize">10</ Property>    </Default-config></C3p0-config>

    Java Code section:

     Packagecom.wang.utils;Importjava.beans.PropertyVetoException;Importjava.sql.Connection;ImportJava.sql.DriverManager;Importjava.sql.PreparedStatement;ImportJava.sql.ResultSet;Importjava.sql.SQLException;ImportCom.mchange.v2.c3p0.ComboPooledDataSource; Public classJdbcutils {Private StaticConnection Conn; Private StaticCombopooleddatasource ds =NewCombopooleddatasource ();  Public StaticConnection getconnection () {Try{conn=ds.getconnection (); } Catch(SQLException e) {e.printstacktrace (); } Catch(propertyvetoexception e) {e.printstacktrace (); }        returnConn; }

    Note that in the configuration file can write multiple database configuration, the above configuration file code, we are placed under the default-config tag, you can add a <named-config name= "mysqlconfig" > tag decoration configuration, Just place the value of name in the new Combopooleddatasource ("Mysqlconfig") in the Code,

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