java database connection pool notes

(the course notes come from knocking with the teacher. The teacher is the black horse program b station white whoring course ~)

#Database connection pool:

1. Concept: it is a container (Collection) for storing data connections

After the container is initialized, the container will be created and some connection objects will be applied in the container. When the user accesses the database, the connection objects will be obtained from the container. After the user accesses, the connection objects will be put back into the container.

2. Benefits:

* save resources

* efficient user access

3. Implementation:

1. Interface: datasourse javax Under SQL package

1. Method:

* get connection: getConnection()

* return Connection: if the Connection object Connection is obtained from the Connection pool, call Connection Close () method, the Connection will not be closed, but will be returned

2. Generally, we do not implement it, but by the database manufacturer

1. c3p0 (older database connection pool technology)

2. Druid: database connection pool technology provided by Alibaba

4,c3p0

Steps:

1. Import jar package # c3p0-0.9.5.2 # mchange-commons-java-0.2.12

2. Define the configuration file:

* Name: c3p0 Properties or c3p0 config xml

* path: directly put the file in the src directory.

3. Create the core object database connection pool object ComboPoolDataSource

4. Get connection: getConnection

 

demo:

 1 package cn.it.c3p0;
 2 
 3 import com.mchange.v2.c3p0.ComboPooledDataSource;
 4 import com.mchange.v2.c3p0.DataSources;
 5 
 6 import javax.sql.DataSource;
 7 import java.sql.Connection;
 8 import java.sql.SQLException;
 9 
10 //c3p0demo
11 public class c3p0_demo {
12     public static void main(String[] args) throws SQLException {
13         //1 creat
14         DataSource ds = new ComboPooledDataSource();
15         //get
16         Connection conn = ds.getConnection();
17         //pr
18         System.out.println(conn);
19     }
20 }

The operation times is wrong

 

 

After watching the barrage, I knew I had used the wrong method. This DataSource is not in the c3p0 package

But only with java ~!!!

Print log information after correction~~

5,Druid:
*Steps:

1. Import the jar package druid-1.0.9 jar

2. Define the configuration file:

* in the form of properties

* you can call any name and put it in any directory. When c3p0 is the name, the system will access it automatically

+: load configuration file properties

3. Get the database connection pool object: get the DruidDataSourceFactory through the factory

4. Get connection: getConnection

package cn.it.Duird;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.naming.spi.DirStateFactory;
import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.util.Properties;

public class DiudDemo {
    public static void main(String[] args) throws Exception {
        //Load profile
        Properties pro = new Properties();
        InputStream is = DiudDemo.class.getClassLoader().getResourceAsStream("druid.properties");
        pro.load(is);
        //Get connection pool object
        DataSource ds = DruidDataSourceFactory.createDataSource(pro);
        //Get connection
        Connection conn = ds.getConnection();
        System.out.println(conn);
    }
}

2. Define tool classes

1. Define a tool class JDBCUtils

2. Provide static code blocks to load configuration files and initialize connection pool objects

3. Providing methods

1. Get connection method: get connection through database connection pool

2. Release resources

3. Method of obtaining connection pool

 

If you need a jar package or any installation package, dd I'll find it and send it ~ ~ hey hey! Learn from each other

Added by Fingers68 on Mon, 31 Jan 2022 00:48:56 +0200