Connection Pooling-c3p0utils Tool Class (C3P0)

Source: Internet
Author: User
Tags connection pooling
First, create a file

Create a file named "C3p0-config.xml" in the SRC directory, which reads:

<?xml version= "1.0" encoding= "UTF-8"?>
<c3p0-config>
  <default-config>
    <property Name= "Driverclass" >com.mysql.jdbc.Driver</property>
    <property name= "Jdbcurl" >jdbc:mysql:// localhost:3306/database name?useunicode=true&amp;characterencoding=utf-8</property>
    <property name= " User ">root</property>
    <property name=" password "> password </property>
    <property name=" Initialpoolsize ">10</property>
    <property name=" MaxIdleTime ">30</property>
    < Property Name= "Maxpoolsize" >100</property>
    <property name= "Minpoolsize" >10</property>
  </default-config>
</c3p0-config>
Second, create C3p0utils
/** * @author CSDN_LQR * @TODO C3P0 Tool Class (Database connection Pooling tool) for managing multiple database connection objects.
 * Unlike DBCP, C3P0 does not have to manually load the configuration file, just put the configuration file into the SRC directory.

    * * public class C3p0utils {//Get a data source (connection pool) private static DataSource ds = new Combopooleddatasource ();
    public static DataSource Getdatasource () {return DS; /** * Get Database Connection Object * * @return/public static Connection getconnection () {Connecti
        On Conn;
            try {conn = ds.getconnection ();
        Return conn; The catch (SQLException e) {throw new RuntimeException ("Server busy ...)
        "); }/** * Close all resource connections * * @param conn * @param PS * @param rs/public static VO
                ID Releaseall (Connection conn, Statement PS, ResultSet rs) {if (conn!= null) {try {
            Conn.close ();
    catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace ();        } conn = null;
            } if (ps!= null) {try {ps.close ();
            catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace ();
        PS = null;
            } if (rs!= null) {try {rs.close ();
            catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace ();
        rs = null;
 }
    }

}
Third, Attention

The jar packages you need to use C3P0 are: C3p0-0.9.1.2.jar, Mysql-connector-java-5.0.8-bin.jar

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.