Imitation of database connection pool, simple imitation, not robust, helps to understand

//Connection pool operation (100 threads, 10 connections)

100 threads are used for the operation of inserting into the database, and 10 connections are 10 connections for the connection pool when initialized

Below is the connection pool class

package database connection pooling my exercise;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ConnctionPool {
	int size;//Connection pool size
	List<Connection> cs = new ArrayList<>();
	
	public ConnctionPool(int size) {
		this.size = size;
		init();
	}
	void init() {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			for (int i = 0; i < size; i++) {
				Connection c = DriverManager.getConnection(
						"jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=UTF-8",
						"root",
						"123");
				cs.add(c);
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace ();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace ();
		}
	}
	// method to get the connection
	synchronized Connection getConnection(){
		while(cs.isEmpty()) {
			try {
				this.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace ();
			}
		}
		Connection c = cs.remove(0);
		return c;
	}
	// method to recycle the connection
	synchronized void returnConnection(Connection c){
		cs.add(c);
		this.notifyAll();
	}
}


The test class is written below. There is a method of writing without a connection pool in the test class. I compared it, and it is indeed more efficient, about three times higher. As far as 100 pieces of data and my computer performance are concerned, the common methods for comparison are: The helper class is not written on

package database connection pooling my exercise;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;

import com.mysql.jdbc.PreparedStatement;

import DAO.User;
import DAO.UserDAO;

public class TestPool {
	public static void main(String[] args) {
		//Connection pool operation (100 threads, 10 connections)
		ConnectionPool cpool = new ConnectionPool(10);
		long start = System.currentTimeMillis();
		for (int i = 0; i < 100; i++) {
			shakeThread e = new excuteThread("线程"+i, cpool);
			e.start ();
			try {
				e.join();
			} catch (InterruptedException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		Date d = new Date();
		long end = d.getTime();
		System.out.println("Time to insert 100 pieces of data using connection pool: "+(end - start));
		
		//Ordinary operation (one hundred threads, the connection is closed after each opening)
		Date d1 = new Date();
		long start1 = d1.getTime();
		for (int i = 0; i < 100; i++) {
			Insert insert = new Insert();
			insert.start();
			try {
				insert.join();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace ();
			}
		}
		Date d2 = new Date();
		long end2 = d2.getTime();
		System.out.println("Common operation time:"+(end2 - start1));
	}
	
	
}

class excuteThread extends Thread{
	ConnectionPool cpool;
	
	public excuteThread(String name,ConnctionPool cpool) {
		super(name);
		this.cpool = cpool;
	}
	
	@Override
	public void run() {
		// get the connection
		Connection c = cpool.getConnection();
		String sql = "insert into user values(?,?,?)";
		System.out.println(this.getName()+"Get the connection and execute the task");
		try (
			PreparedStatement ps =  (PreparedStatement) c.prepareStatement(sql);
			)
			{
			ps.setInt(1, 6);
			ps.setString(2, "Jay Chou");
			ps.setInt(3, 38);
			
			//100 threads insert 100 pieces of data
			ps.execute();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace ();
		}
		//Recycle the connection
		cpool.returnConnection(c);
	}
}

class Insert extends Thread{
	@Override
	public void run() {
		
		UserDAO userDao = new UserDAO();
		User user = new User(66, "小春", 38);
		userDao.add(user);
	}
	
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325522348&siteId=291194637