C3P0 of JDBC learning notes

step

  1. Import jar package
    Import c3p0-0.9.5.2.jar and mchange-commons-java-0.2.12.jar
  2. Define profile
    Name: c3p0.properties or c3p0-config.xml
    Path: put the file directly in the src directory
<c3p0-config>
  <!-- Read connection pool objects using the default configuration -->
  <default-config>
  	<!--  Connection parameter -->
    <property name="driverClass">com.mysql.cj.jdbc.Driver</property>
    <property name="jdbcUrl">jdbc:mysql://localhost:3306/test04</property>
    <property name="user">root</property>
    <property name="password">root</property>
    
    <!-- Connection pool parameters -->
    <!--Number of connections requested for initialization-->
    <property name="initialPoolSize">5</property>
    <!--Maximum connections-->
    <property name="maxPoolSize">10</property>
    <!--Timeout time-->
    <property name="checkoutTimeout">3000</property>
  </default-config>

  <named-config name="otherc3p0"> 
    <!--  Connection parameter -->
    <property name="driverClass">com.mysql.cj.jdbc.Driver</property>
    <property name="jdbcUrl">jdbc:mysql://localhost:3306/test04</property>
    <property name="user">root</property>
    <property name="password">root</property>
    
    <!-- Connection pool parameters -->
    <property name="initialPoolSize">5</property>
    <property name="maxPoolSize">8</property>
    <property name="checkoutTimeout">1000</property>
  </named-config>
</c3p0-config>

We do not need to use the code after < named config name = "otherc3p0" > at present, but the meaning of the previous code can be seen in the comments
3. Create core object database connection pool object ComboPooledDataSource

//1. Create database connection pool object
DataSource ds = new ComboPooledDataSource();
  1. Get connection: getConnection
//2. Get connection object
Connection conn = ds.getConnection();
  1. Print conn objects
//3. printing
System.out.println(conn);

Overall Code:

package com.zzq.c3p0;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * c3p0 Demonstration
 */
public class C3P0Demo01 {
    public static void main(String[] args) {
        //1. Create database connection pool object
        DataSource ds = new ComboPooledDataSource();
        try {
            //2. Get connection object
            Connection conn = ds.getConnection();
            //3. printing
            System.out.println(conn);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

Operation effect:

Note: the red part is not error reporting but log information, and the red box is conn

C3P0 advanced

Let's verify the maximum number of connections in the database connection pool

Let's get 10 first

for (int i = 1; i <= 10; i++) {
            try {
                Connection conn = ds.getConnection();
                System.out.println(i + ":" + conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

You can see that we got 10 conn objects with different hash values

So what if we get 11?
It will acquire 10 successfully, wait for 3 seconds, and then report an error


If I still get 10, but we get the fifth and return it

for (int i = 1; i <= 11; i++) {
            try {
                Connection conn = ds.getConnection();
                System.out.println(i + ":" + conn);
                if (i == 5) {
                    conn.close(); //Return connection to connection pool
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

As you can see, there is no wrong report

Let's take a look at the xml file. We can see that in addition to the code we just used, there are still some parts we haven't mentioned:

In fact, the first is the default xml configuration file. If you do not specify which one to use, the first one will be used by default, as follows:

If we specify which one to use, we will use which one, as follows:

 DataSource ds = new ComboPooledDataSource("otherc3p0");
Published 42 original articles, won praise 1, visited 181
Private letter follow

Keywords: MySQL JDBC Database Java

Added by j007w on Wed, 19 Feb 2020 10:45:48 +0200