JDBC (Connection Pool)

 
 
package zucc.edu.cn.jdbc.datasource;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.LinkedList;

public class MyDataSource {
	private String url = "jdbc:mysql://localhost:3306/jdbc";
	private String user = "root";
	private String password = "19980102";
	private static int maxCount = 10; // The maximum number of connections in the connection pool
	private static int initCount = 5; // Connection pool initialization number of connections
	private static int currentCount = 0; // The current number of connections in the connection pool

	private LinkedList<Connection> connectionPool = new LinkedList<Connection>();
	/*
	 * Because you need to insert and delete the connection pool frequently, use LinkedList
	 */

	public MyDataSource() {
		/*
		 * It is very expensive to create at the beginning, but it is very fast to establish a connection and access the database later because it is operating memory
		 */
		for (int i = 0; i < initCount; i++) {
			try {
				this.connectionPool.addLast(this.createConnection());
				this.currentCount++;
			} catch (SQLException e) {
				throw new ExceptionInInitializerError(e);
			}
		}
	}

	public Connection getConnection() throws SQLException {
		synchronized (connectionPool) {
			if (this.connectionPool.size() > 0) {
				return this.connectionPool.removeFirst();
			}

			if (this.currentCount < maxCount) {
				this.currentCount++;
				return this.createConnection();
			}
			/*
			 * If there are still connections in the connection pool, use removeFirst to get the connection. If the current number of connections is less than the maximum number of connections, create a connection
			 */

			throw new SQLException("No connection");
		}
	}

	private Connection createConnection() throws SQLException {
		return DriverManager.getConnection(url, user, password);
	}

	/*
	 * Cancel the link to the database, and then put the canceled link into the connection pool
	 */
	public void free(Connection conn) {
		this.connectionPool.addLast(conn);
	}
}

At the same time, other classes need to be modified

public final class jdbcUtils { // package tool class
	private String url = "jdbc:mysql://localhost:3306/jdbc";
	private String user = "root";
	private String password = "19980102";
	private static MyDataSource myDataSource = null; //************Changes**********

	private static jdbcUtils instance = null;// Singleton design mode, only register the driver once

	private jdbcUtils() {
	}

	public static jdbcUtils getinstance() {
		if (instance == null) { // lazy loading
			instance = new jdbcUtils();
		}
		return instance;
	}

	static {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			myDataSource = new MyDataSource(); //************Changes**********
		} catch (ClassNotFoundException e) {
			e.printStackTrace ();
		}
	}

	public Connection getConnection() throws SQLException {
		// return DriverManager.getConnection(url, user, password);
		return myDataSource.getConnection(); //************Changes**********
	}

	public static void free(ResultSet rs, Statement st, Connection conn) {
		try {
			if (rs != null) {
				rs.close();
			}
		} catch (SQLException e) {
			e.printStackTrace ();
		} finally {
			try {
				if (st != null) {
					st.close();
				}
			} catch (SQLException e) {
				e.printStackTrace ();
			} finally {
				if (conn != null) {
					try {
						// conn.close();
						myDataSource.free(conn); //************Changes**********
					} catch (Exception e) {
						e.printStackTrace ();
					}
				}
			}
		}
	}
}

main function

public static void main(String[] args) throws ClassNotFoundException, SQLException {
		// template();
		for (int i = 0; i < 10; i++) {
			Connection conn = jdbcUtils.getinstance().getConnection();
			System.out.println(conn);
			jdbcUtils.free(null, null, conn);
		}
	}

If the main function does not release the link

public static void main(String[] args) throws ClassNotFoundException, SQLException {
		// template();
		for (int i = 0; i < 11; i++) {
			Connection conn = jdbcUtils.getinstance().getConnection();
			System.out.println(conn);
			// jdbcUtils.free(null, null, conn);
		}
	}

Results of the

com.mysql.jdbc.JDBC4Connection@1698c449
com.mysql.jdbc.JDBC4Connection@5ef04b5
com.mysql.jdbc.JDBC4Connection@5f4da5c3
com.mysql.jdbc.JDBC4Connection@443b7951
com.mysql.jdbc.JDBC4Connection@14514713
com.mysql.jdbc.JDBC4Connection@2328c243
com.mysql.jdbc.JDBC4Connection@108c4c35
com.mysql.jdbc.JDBC4Connection@3fa77460
com.mysql.jdbc.JDBC4Connection@e2144e4
com.mysql.jdbc.JDBC4Connection@573fd745
Exception in thread "main" java.sql.SQLException: No connection
	at zucc.edu.cn.jdbc.datasource.MyDataSource.getConnection(MyDataSource.java:49)
	at zucc.edu.cn.jdbc.jdbcUtils.getConnection(jdbcUtils.java:40)
	at zucc.edu.cn.jdbc.Base.main(Base.java:12)

Guess you like

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