JDBC学习之路(十)编写一个基本的连接池来实现连接的复用

18 篇文章 0 订阅

众所周知,在JDBC创建或者使用的过程中,第一步创建连接是最复杂的也是最耗费时间的,因为它是一个网络操

作,需要不断的去尝试连接地址,所以建立好一个连接以后仅仅发送少量的sql语句就将其关闭是很不划算的,所以这

就诞生了连接池这个概念。所谓的连接池就是将多个创建好的连接放入到一个容器中,当使用的时候就从这个容器中

取出来连接,使用完成之后再把这个连接放回到容器中去,这样就完成了连接的复用,这样的想法是一个简单的连接池

的实现,当然了呃,真正的连接池要考虑的因素还很多,我们这里只是说说最简单的和想法,下面来说明一下这个最简

单的连接池的开发。

 

package com.bird.jdbc;

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

/**
 * 一个简单的连接池的实现
 * @author Bird
 *
 */
public class MyDataSource {
	private static String url = "jdbc:mysql://localhost:3306/test";
	private static String username = "root";
	private static String password = "mysql";
	
	private static int initCount = 5;
	private static int maxCount = 10;
	private static int currentCount = 0;

	private LinkedList<Connection> connectionsPool = new LinkedList<Connection>();

	public MyDataSource() {
		for (int i = 0; i < initCount; i++) {
			try {
				this.connectionsPool.add(creatConnection());
				currentCount++;
			} catch (SQLException e) {
				throw new ExceptionInInitializerError(e);
			}
		}
	}
	
	
	public Connection getConnection() throws SQLException{//从连接池中拿走一个连接
		synchronized (connectionsPool) {
			if(connectionsPool.size() > 0)
			return connectionsPool.remove();
			
			if(connectionsPool.size() < maxCount){
				currentCount++;
				return this.creatConnection();
			}
		}
		throw new SQLException("超过最大连接");
	
	}
	
	
	public void free(Connection conn){//释放连接,把连接放到连接池中
		this.connectionsPool.add(conn);
	}
	
	private Connection creatConnection() throws SQLException {
		return DriverManager.getConnection(url, username, password);
	}
}


 

然后对于这个使用也是很简单,就是把这些连接更改在工具类中就算可以了,这体现了工具类的重要

package com.bird.jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
 * JDBC工具类
 * @author bird
 *
 */
public final class Temple {
	private static MyDataSource dataSource;
	
	private Temple() {
	}

	static {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			dataSource = new MyDataSource();
		} catch (ClassNotFoundException e) {
			throw new ExceptionInInitializerError(e);
		}
	}

	public static Connection getConnection() throws SQLException {
		return dataSource.getConnection();
		
	}

	public static void free(Connection con, Statement st, ResultSet rs) {
		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 (con != null)
					dataSource.free(con);
			}
		}
	}

	

}

就是在需要关闭连接的时候不把他关闭,而是仅仅把它放入到容器中,就这样而已。我们测试了一下,连续取出10次

连接,是可以发现有复用的连接的

com.mysql.jdbc.JDBC4Connection@a39137
com.mysql.jdbc.JDBC4Connection@92e78c
com.mysql.jdbc.JDBC4Connection@9fbe93
com.mysql.jdbc.JDBC4Connection@198dfaf
com.mysql.jdbc.JDBC4Connection@1858610
com.mysql.jdbc.JDBC4Connection@a39137
com.mysql.jdbc.JDBC4Connection@92e78c
com.mysql.jdbc.JDBC4Connection@9fbe93
com.mysql.jdbc.JDBC4Connection@198dfaf
com.mysql.jdbc.JDBC4Connection@1858610


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值