"Javaweb" C3P0 connection pool with MySQL

Source: Internet
Author: User
Tags connection pooling

Before the text

In the previous article, we talked about the traditional way of JDBC connecting MySQL, but it was inefficient and obviously not as efficient as the connection pool when making multiple connections, so let's explain one of the JDBC connection pools this time: c3p0

Body 1. Preparatory work
    • IntelliJ idea
    • c3p0-0.9.5.2 (JAR)
    • Mysql
2. Configure C3p0-config.xmldefault-config

Before you configure, you need to work on establishing a database, or using a database named customer in the previous article

Then create a new XML file named C3p0-config in the src directory of the project (the file name cannot be customized)

Then start the configuration:

        <!-- 数据库地址 -->        <property name="jdbcUrl">jdbc:mysql://localhost:3306/customer</property>        <!-- 数据库驱动 -->        <property name="driverClass">com.mysql.jdbc.Driver</property>        <!-- 用户名 -->        <property name="user">root</property>        <!-- 密码 -->        <property name="password">3865933</property>

First write the basic database information, and then we write the common connection pool properties, to query all properties, you can read the official website guide, the properties are also included in the

        <!-- 连接池初始化时创建的连接数 -->        <property name="initialPoolSize">1</property>        <!-- 连接池中的最小连接数 -->        <property name="minPoolSize">1</property>        <!-- 连接池中的最小连接数 -->        <property name="maxPoolSize">2</property>        <!-- 无空闲连接可用时,可一次性创建新连接的数量 -->        <property name="acquireIncrement">1</property>        <!-- 获取数据库连接失败后重复尝试的次数 -->        <property name="acquireRetryAttempts">1</property>        <!-- 两次连接的间隔时间,单位为毫秒 -->        <property name = "acquireRetryDelay">1000</property>

Give a complete picture of the file:

Named-config

You can add named-config to increase the database configuration, the content is consistent with default-config, and I connect to another database product:

3. Write a Demo

Take care of the data source first:

    private static ComboPooledDataSource dataSource1 = new ComboPooledDataSource("test");    private static ComboPooledDataSource dataSource2 = new ComboPooledDataSource();

Connect according to the data source

    private static void getConnection(){        try{            Connection connection = dataSource1.getConnection();            System.out.println("连接数据库 product 成功!");            connection.close();            System.out.println("数据库连接已关闭!");        }catch(Exception e){            System.out.println("连接数据库失败!");        }        try{            Connection connection = dataSource2.getConnection();            System.out.println("连接数据库 customer 成功!");            connection.close();            System.out.println("数据库连接已关闭!");        }catch(Exception e){            System.out.println("连接数据库失败!");        }    }    public static void main(String[] args){        getConnection();    }

Operation Result:

If you need to have more than one connection, the use of connection pooling is the perfect choice, the explanation about C3P0 is over, thank you.

"Javaweb" C3P0 connection pool with MySQL

Related Article

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.