JDBC(连接池)

 
 
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; // 连接池最大连接数
	private static int initCount = 5; // 连接池初始化连接数
	private static int currentCount = 0; // 连接池目前连接数

	private LinkedList<Connection> connectionPool = new LinkedList<Connection>();
	/*
	 * 因为要经常对连接池进行插入删除操作,所以用LinkedList
	 */

	public MyDataSource() {
		/*
		 * 一开始创建成本很高,但是以后建立连接并访问数据库因为是操作内存所以速度很快
		 */
		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();
			}
			/*
			 * 如果连接池里还有连接则用removeFirst取连接 如果当前连接数小于最大连接数,则创建连接
			 */

			throw new SQLException("已没有连接");
		}
	}

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

	/*
	 * 取消对数据库的链接,然后把取消的这个链接放入连接池中
	 */
	public void free(Connection conn) {
		this.connectionPool.addLast(conn);
	}
}

同时需要对其他的类进行修改

public final class jdbcUtils { // 封装工具类
	private String url = "jdbc:mysql://localhost:3306/jdbc";
	private String user = "root";
	private String password = "19980102";
	private static MyDataSource myDataSource = null; //*********更改处************

	private static jdbcUtils instance = null;// 单例设计模式,只注册一次驱动

	private jdbcUtils() {
	}

	public static jdbcUtils getinstance() {
		if (instance == null) { // 懒加载
			instance = new jdbcUtils();
		}
		return instance;
	}

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

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

	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); //*********更改处************
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}
	}
}

主函数

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);
		}
	}

如果主函数不释放链接的话

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);
		}
	}

执行结果

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: 已没有连接
	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)

猜你喜欢

转载自blog.csdn.net/qq_36858117/article/details/80017265